Home > database >  SAS macro variable as diffrent date informat/format
SAS macro variable as diffrent date informat/format

Time:06-24

I want to put one date as a variable and I want to convert to a different format (sas informant). I need those to become a char to concatenate with the table name.

%LET GL_YMD= %sysfunc(mdy(1,31,2009),date9.);/*expeced result GL_YMD=31JAN2009*/
%LET YYYYMMDD= %sysfunc(inputn(&GL_YMD,YYMMDD8.)); /*expeced result YYYYMMDD=20090131*/
%LET YYYYMM= %sysfunc(inputn(&GL_YMD,YYYYMM));  /*expeced result YYYYMM=200901*/

I read lots of tutorials on this topic, it seems it should work but it: I get a response: NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release. Inserting white space between a quoted string and the succeeding identifier is recommended.

CodePudding user response:

Did not get the NOTE but I did get warnings from INPUTN function. You need PUTN function. Note the use of date literal.

45         %LET GL_YMD= %sysfunc(mdy(1,31,2009),date9.);/*expeced result GL_YMD=31JAN2009*/
46         %put &=GL_Ymd;
GL_YMD=31JAN2009
47         %LET YYYYMMDD= %sysfunc(putn("&GL_YMD"d,YYMMDDn8.)); /*expeced result YYYYMMDD=20090131*/
48         %put &=yyyymmdd;
YYYYMMDD=20090131
49         %LET YYYYMM= %sysfunc(putn("&GL_YMD"d,YYMM.));  /*expeced result YYYYMM=200901*/
50         %put &=yyyymm;
YYYYMM=2009M01

CodePudding user response:

inputn() converts characters to a number. You want to use putn() instead, and specify the date as a date literal, "&GL_YMD"d. You've converted your date to a character date9 format, and these functions expect a SAS date number.

Note you should also be using yymmddn8. as your and yymmn6. as your formats to get your expected results.

%LET GL_YMD= %sysfunc(mdy(1,31,2009), date9.); 
%put &gl_ymd; /* 31JAN2009 */

%LET YYYYMMDD= %sysfunc(putn("&GL_YMD."d, YYMMDDN8.)); /* 20090131 */
%put &yyyymmdd; /* 20090131 */

%LET YYYYMM= %sysfunc(putn("&GL_YMD"d, YYMMN6.)); 
%put &yyyymm.; /* 200901 */

"&GL_YMD"d is equivalent to:

%sysfunc(inputn(&GL_YMD., date9.) );

Note that this is a special case. The function can work this out correctly because it is designed to parse date literals, but as far as the macro compiler is concerned, it's just text. If you put "&GL_YMD"d in the log, you will see exactly that. It can be a confusing rule but it's helpful to know for date functions in macros.

CodePudding user response:

Formats convert values to text. You use formats with the PUT(), PUTN(), and PUTC() functions and the PUT and FORMAT statements. Numeric formats convert numbers to text. Character formats convert character values to text.

Informats convert text to values. You use informats with the INPUT(), INPUTN(), and INPUTC() functions and the INPUT and INFORMAT statements. Numeric informats convert text to numbers. Character informats convert text to character strings.

You first used the MDY() function and the DATE9. format specification to create the string 31JAN2009. You then tried to use the INPUTN() function to interpret that string as if it was in the style recognized by the YYMMDD informat. But that informat will not understand the letter JAN. And even if had worked it would have returned the internal number of days used to represent that date, and not the stylized YYYYMMDD string you claimed to want.

So use the PUTN() function instead with a FORMAT instead of an INFORMAT. The FORMAT specification that will make a string in the style YYYYMMDD is YYMMDDN8. and the format that will make a string in the style YYYYMM is the YYMMN6. format.

%let date=%sysfunc(mdy(1,31,2009));
%let GL_YMD=%sysfunc(putn(&date,date9.));
%let YYYYMMDD=%sysfunc(putn(&date,YYMMDDN8.)); 
%let YYYYMM=%sysfunc(putn(&date,YYMMN6.));

Result:

110  %put &=date &=gl_ymd &=yyyymmdd  &=yyyymm ;
DATE=17928 GL_YMD=31JAN2009 YYYYMMDD=20090131  YYYYMM=200901

If you want to have SAS interpret the string 31JAN2009 as a date value you need to convert it to a date literal (a quoted string that the DATE informat can interpret followed immediately by the letter d). So you could also use the string in GL_YMD to represent a date like this:

%let YYYYMMDD=%sysfunc(putn("&gl_ymd"d,YYMMDDN8.)); 
  • Related