BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SasStatistics
Pyrite | Level 9

I am creating a macro variable of the form: 

Data _NULL_;
 ThisDate=Date();
 call symput("FirstDayTwoMonthBack",(IntNX("Month", ThisDate, -2, 'beginning')));
Run;

Then when you display the value of this macro variable it is just a "number", i would like to store it as a date format of the type: "2021-11-29" which means year 2021, month = 1, and day = 29. 

Any advice on how to do this? 


1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

As others already mentioned I would keep it as a number. I would also use CALL SYMPUTX to avoid notes about type conversion.

 

The functions of the DATA Step can also be used in the SAS macro language using %SYSFUNC. Have a look at the below code sample.

 

data _null_;
 ThisDate=Date();
 call symputx("FirstDayTwoMonthBack", IntNX("Month", ThisDate, -2, 'beginning'));

run;
%put NOTE: &FirstDayTwoMonthBack;
%put NOTE: %sysfunc(putn(&FirstDayTwoMonthBack, yymmddd10.));

%let newDate = %sysfunc(intnx(month, %sysfunc(today()), -2, b));
%put NOTE: &=newDate;

 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

usually, it is unnecessary to format a macro variable. the un-formatted macro variable works fine in all logical and arithmetical situations.

 

The only time you need to format a macro variable is when you are going to use it in a title, label or file name.

 

--
Paige Miller
maguiremq
SAS Super FREQ
Data _NULL_;
 ThisDate=Date();
 call symput("FirstDayTwoMonthBack", put(IntNX("Month", ThisDate, -2, 'beginning'), yymmdd10.));
Run;
%put &FirstDayTwoMonthBack;
2021-09-01

I agree with @PaigeMiller that this isn't totally necessary, but that gives you what you want I think.

PaigeMiller
Diamond | Level 26

Please remember, @SasStatistics , that if you format the macro variable value, and you want to use it for arithmetic or logical purposes, then you will have to un-format it. That's two operations (formatting followed by un-formatting, and don't forget to do both, and don't get one of the operations wrong) instead of zero operations. Either will work if done properly, it's your choice.

--
Paige Miller
BrunoMueller
SAS Super FREQ

As others already mentioned I would keep it as a number. I would also use CALL SYMPUTX to avoid notes about type conversion.

 

The functions of the DATA Step can also be used in the SAS macro language using %SYSFUNC. Have a look at the below code sample.

 

data _null_;
 ThisDate=Date();
 call symputx("FirstDayTwoMonthBack", IntNX("Month", ThisDate, -2, 'beginning'));

run;
%put NOTE: &FirstDayTwoMonthBack;
%put NOTE: %sysfunc(putn(&FirstDayTwoMonthBack, yymmddd10.));

%let newDate = %sysfunc(intnx(month, %sysfunc(today()), -2, b));
%put NOTE: &=newDate;

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2615 views
  • 8 likes
  • 4 in conversation