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

Hi,

 

I have the following code:

 

%let Report_Date = '31Dec2017'd;
%let yesterday= '30Dec2017'd;
%let last_week = '24Dec2017'd;
DATA example&Report_Date.;
set example;
/* do stuff */
RUN;
DATA example_compare_yesterday;
set example&Report_Date. example&yesterday.;
RUN;
DATA example_compare_last_week;
set example&report_Date. example&last_week.;
RUN;

  

I would like to make this code iterative, but don't have much of a grounding in macros (which I assume would be appropriate).

 

I am looking to do the following:

On any given date, "Report_Date" will have the current date

I would like to automatically populate "yesterday" and "last week" with the relevant date such that I can generate the comparative datasets.

 

Currently I have to populate all three dates to make this work.

 

Any help would be appreciated.

 

Thanks!

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Look at INTNX to increment yoru dates automatically. 
Remember that you can use CALL SYMPUTX within a DATA _NULL_ step to create your macro variables, if that's easier. 

 

 

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

 

I think this has relevant examples for you - including how to loop through dates if desired. 

 

You may need to tweak your code a bit to handle the unquoted/d on the date variable. YOu can either leave them as numbers and trust SAS is doing it correctly, or you can use DATE9 Format to create them as DATE9 date values and quote/add D in the report code instead. The second approach is a bit more user friendly;

data _null_;

report_date = '31Dec2017'd;
yesterday = report_Date - 1;
last_week = report_date - 7;

call symputx('Report_Date', report_date);
call symputx('myDate', put(Report_date, date9.)); ..... .... run; %put &report_date;

 

 

View solution in original post

2 REPLIES 2
Reeza
Super User

Look at INTNX to increment yoru dates automatically. 
Remember that you can use CALL SYMPUTX within a DATA _NULL_ step to create your macro variables, if that's easier. 

 

 

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

 

I think this has relevant examples for you - including how to loop through dates if desired. 

 

You may need to tweak your code a bit to handle the unquoted/d on the date variable. YOu can either leave them as numbers and trust SAS is doing it correctly, or you can use DATE9 Format to create them as DATE9 date values and quote/add D in the report code instead. The second approach is a bit more user friendly;

data _null_;

report_date = '31Dec2017'd;
yesterday = report_Date - 1;
last_week = report_date - 7;

call symputx('Report_Date', report_date);
call symputx('myDate', put(Report_date, date9.)); ..... .... run; %put &report_date;

 

 

MikeFranz
Quartz | Level 8
This is great, thank you! I didn't know about this data _null_ option, very useful!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 867 views
  • 0 likes
  • 2 in conversation