Thursday, October 13, 2022
HomeData ScienceUtilizing SAS Macro Variable As Date

Utilizing SAS Macro Variable As Date


We’ve now mentioned changing a textual content string (both hardcoded textual content or a variable) right into a date variable. Nevertheless, you might have a macro variable that appears like a date.

On this article, we assume the next earlier than we convert a SAS macro variable as date.

Using SAS macro variable as date
  1. You will have a macro variable that appears like a date (e.g., 31DEC2020), and
  2. You wish to create a brand new macro variable that incorporates the numeric illustration of this date (e.g., 22280)
%let mydate = 2021-12-31;
%put unique worth: &mydate;
%let numeric_date= %sysfunc(inputn(&mydate, yymmdd10.));
%put numeric date : &numeric_date;
unique worth: 2021-12-31
 71         %let numeric_date= %sysfunc(inputn(&mydate, yymmdd10.));
 72         %put numeric date : &numeric_date;
 numeric date : 22645

You utilize SYSFUNC and INPUTN to vary a macro variable right into a date.

First, you need to use SAS capabilities outdoors a SAS Data Step or PROC SQL with the SYSFUNC function. Then, you use the INPUTN function to turn the macro variable that looks like a date into an actual macro date.

To display the formatted SAS date in the log, you can use the

%let date_d9=%sysfunc(inputn(&mydate, yymmdd10.), date9.);
%put date9 format. : &date_d9;

%let date_d9=%sysfunc(inputn(2021-12-31, yymmdd10.), date9.);
%put date9 format. : &date_d9;

Note that the value passed in the input function should not be in quotes.

The INPUTN function is similar to the INPUT function because both convert text into numbers. However, you can use the INPUTN function in a %LET statement, whereas you can’t do this with the INPUT function.

The format of macro variables can be changed with the INPUTN, INPUTC or PUTN, PUTC functions.

To change a macro variable using numeric informat, use the INPUTN function.

To change a macro variable using a character format, use the PUTC function.

If you need to print it in a human-readable format in which case you can always use %SYSFUNC(PUTN(...))

Syntax:

val = %SYSFUNC(INPUTC(char val, informat));
val = %SYSFUNC(INPUTN(num val, informat));
val = %SYSFUNC(PUTC(char val, format));
val = %SYSFUNC(PUTC(num val, format))

Using macro dates as subsetting if OR WHERE statement

%let dt=01JAN2000;
data dtates2;
	set sashelp.rent;
	where date gt &DT;
run;

This throws an error because &dt is stored as a character.

Using SAS macro variable as date

The code works fine by adding a date literal in the macro variable.

%let dt='01JAN2000'D;
data dtates2;
	set sashelp.rent;
	where date gt &DT;
run;

Formatted SAS dates, which are text, must be enclosed in single or double quotes, followed by the letter D (since you are using a macro variable, it must be double quotes)

It is important to create a new variable for conversion. By using the same variables as before, variables will remain characters.

What if the macro variable contains a numeric representation of the date?

proc sql noprint;
select min(date) into :mindatel from
sashelp.buy;
quit;
%put &mindatel;
13149

The example below results in an error because the value of &mindate is a number. Even if you apply quotes, it results in an error.

proc sql;
select * from sashelp.rent
where date > &mindate;
quit;

proc sql;
select * from sashelp.rent
where date > "&mindate";
quit;
ERROR: Expression using greater than (>) has components that are of different data types.

Solution:

The solution is to add a date format to the macro variable while creating it and add the date literal while resolving the variable.

proc sql noprint;
select min(date) format = date9. into :mindate from
sashelp.buy;
quit;

proc sql;
select * from sashelp.rent
where date > "&mindate"D;
quit;
%put value is &mindate;



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments