DATA Step, Macro, Functions and more

Creating and referencing dynamically named datasets

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

Creating and referencing dynamically named datasets

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!

 

 

 

 

 


Accepted Solutions
Solution
‎11-02-2017 04:56 AM
Super User
Posts: 23,228

Re: Creating and referencing dynamically named datasets

Posted in reply to MikeFranz

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


All Replies
Solution
‎11-02-2017 04:56 AM
Super User
Posts: 23,228

Re: Creating and referencing dynamically named datasets

Posted in reply to MikeFranz

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;

 

 

Contributor
Posts: 44

Re: Creating and referencing dynamically named datasets

This is great, thank you! I didn't know about this data _null_ option, very useful!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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