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

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.

 

datedate_monthyear
3/3/20183/3/20183032018
3/10/20183/10/20183102018
3/17/20183/17/20183172018
3/24/20183/24/20183242018
3/31/20183/31/20183312018

 

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.); 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SuryaKiran
Meteorite | Level 14

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;
Thanks,
Suryakiran

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Astounding
PROC Star

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, ')'));

 

 

SuryaKiran
Meteorite | Level 14

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;
Thanks,
Suryakiran
Spintu
Quartz | Level 8
Thank you for giving me a good sugesstion. I implemented it's good pratice.
One more clarification in that step can I execute proc append. If so then first i need to create a seperate proc append macro and pass into this same call execute step. Is that make sense?

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
  • 4 replies
  • 748 views
  • 3 likes
  • 4 in conversation