DATA Step, Macro, Functions and more

How to loop data values into a diffreent macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

How to loop data values into a diffreent macro

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

 


Accepted Solutions
Solution
‎04-13-2018 10:32 PM
Valued Guide
Posts: 559

Re: How to loop data values into a diffreent macro

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


All Replies
Super User
Super User
Posts: 9,416

Re: How to loop data values into a diffreent macro

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.

Super User
Posts: 6,632

Re: How to loop data values into a diffreent macro

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

 

 

Solution
‎04-13-2018 10:32 PM
Valued Guide
Posts: 559

Re: How to loop data values into a diffreent macro

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
Occasional Contributor
Posts: 15

Re: How to loop data values into a diffreent macro

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?
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 113 views
  • 3 likes
  • 4 in conversation