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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 641 views
  • 0 likes
  • 2 in conversation