The below output datasets, I would like to create do loop (month and year) , so that it will iterate total no of times . Can you please someone help me?
so that these values I can pass into a different macro.
date | date_ | month | year |
3/3/2018 | 3/3/2018 | 303 | 2018 |
3/10/2018 | 3/10/2018 | 310 | 2018 |
3/17/2018 | 3/17/2018 | 317 | 2018 |
3/24/2018 | 3/24/2018 | 324 | 2018 |
3/31/2018 | 3/31/2018 | 331 | 2018 |
SAS Code:
data want;
sub_table_date='01MAR2018'd;
end_date=intnx('month', sub_table_date, 0, 'e');
do date=sub_table_date to end_date;
if weekday(date)=7 then output ;
end;
format date mmddyy10.;
keep date;
run;
data test;
set want;
date_= put(date, mmddyy10.);
month=trim(compress(substr(date_,1,5),'/'));
year= substr(date_,7,4);
call symput( 'mon',month);
call symput( 'yr',year);
run;
Finally I would like pass this 'mon' and 'yr' value into a do loop statement.
%visit(&mon.,&yr.);
Why not in a single data step? Your converting date values to character and passing them as parameters for a macro. I suggest using @Astounding approach with in do loop like:
%Let date=01MAR2018;
Data _NULL_;
do i="&date"D to intnx('month', "&date"D, 0, 'e');
if weekday(i)=7 then call execute (cats(%nrstr('%visit'), '(', put(i,mmddyy4.), ',' , put(year(i),NLDATEYR4.), ')'));
end;
run;
Why? This should actually be the first question you always ask yourself when you state use macro. Base SAS can do anything you need with simpler, easier to maintain code. Macro is just a find replace system. There are methods to do what you ask, call execute for instance, but present your problem - show some test data in the form of a datastep and what the required output would be. Its quite likely as simple addition of a by group would remove this need totally.
While it might be possible to combine the two DATA steps, let's work with them as they are now. First, remove the two CALL SYMPUT statements. Then add in their place:
call execute (cats(%nrstr('%visit'), '(', month, ',' , year, ')'));
Why not in a single data step? Your converting date values to character and passing them as parameters for a macro. I suggest using @Astounding approach with in do loop like:
%Let date=01MAR2018;
Data _NULL_;
do i="&date"D to intnx('month', "&date"D, 0, 'e');
if weekday(i)=7 then call execute (cats(%nrstr('%visit'), '(', put(i,mmddyy4.), ',' , put(year(i),NLDATEYR4.), ')'));
end;
run;
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.
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.