SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shidehrafigh
Fluorite | Level 6

Hi everyone

 

I am struggling with creating a macro that loops through a date range that is specified.

I would like to give my macro a start date and end date and run a piece of macro to automatically create data sets.

the isse is where I have to define the second date where it starts the iteration:

%let date=%substr(%sysfunc(intnx(month,%substr(%sysfunc(inputn(&start,yymmn6.),yymmn6.),1,6),1,yymmn6.),yymmn6.),1,6);

 

I get the following warning:

WARNING: Argument 4 to function INTNX referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The result of the operations have been set to a missing value.

 


options symbolgen;
%let start='20181101'd;
%let end='20190201'd;
data _null_;
datestart = input("&start",yymmn6.);
dateend = input("&end",yymmn6.);
counter = intck('month',datestart,dateend)+1;
call symput('counter',put(counter,best.));
run;

%macro loop;
%let date=%substr(%sysfunc(inputn(&start,yymmn6.),yymmn6.),1,6);
%do i=1 %to &counter;
data data_&date.;
set data_&date.;
run;
%let date=%substr(%sysfunc(intnx(month,%substr(%sysfunc(inputn(&start,yymmn6.),yymmn6.),1,6),1,yymmn6.),yymmn6.),1,6);
%put &i &counter &date;
%end;
%mend loop;
%loop

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Maybe this can help.

https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n01vuhy8h909xgn16p0x6rddpoj9.htm&docse...

 

I'm also assuming that's partial code, because what you're doing there won't change anything.

 

View solution in original post

3 REPLIES 3
Reeza
Super User

Maybe this can help.

https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n01vuhy8h909xgn16p0x6rddpoj9.htm&docse...

 

I'm also assuming that's partial code, because what you're doing there won't change anything.

 

shidehrafigh
Fluorite | Level 6

Hi Reeza

 

Thanks a lot! I looked for a documentation for quite a long time but I couldn't find something that would address the date issue.

 

I used the same code to create the previous month data for each month as well.

 

 

Regards,

Shideh

Tom
Super User Tom
Super User

First make sure you start with valid values. Date literals must be a a format that the DATE informat can understand.

%let start='01NOV2018'd;
%let end='01FEB2019'd;

If you want to loop in macro code then increment using a normal integer based %DO loop over the offset number and use it generate a valid date.

%macro test(from,to);
%local i date_raw date_fmt;
%do i=0 %to %sysfunc(intck(month,&from,&to));
  %let date_raw=%sysfunc(intnx(month,&from,&i));
  %let date_fmt=%sysfunc(intnx(month,&from,&i),date9);
  %put &=i &=date_fmt &=date_raw;
%end;
%mend test;
267   %test(&start,&end);
I=0 DATE_FMT=01NOV2018 DATE_RAW=21489
I=1 DATE_FMT=01DEC2018 DATE_RAW=21519
I=2 DATE_FMT=01JAN2019 DATE_RAW=21550
I=3 DATE_FMT=01FEB2019 DATE_RAW=21581

If you just want to use the value in code then just use the raw number of days.  If you want to use a value like 01FEB2019 in code then you need to enclose it in quotes and add a D suffix.

"&date_fmt"d

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 3 replies
  • 4709 views
  • 2 likes
  • 3 in conversation