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

Hello, I want to make a loop with this code, I have many dates just put 2 dates below:

 

%let cdate=31oct2020;
%put &cdate;

data new_&cdate.;
format dat date9.;
dat="&cdate"d;

run;

%let cdate=31jan2021 ;
%put &cdate;

data new_&cdate.;
format dat date9.;
dat="&cdate"d;

run;

 

Thanks for your hepl

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

I have lost the thread of what the question is.

 

If you want to do the same thing over a list of values then in MACRO code the easiest way is to put the list of values into a macro variable.  Since your example appear to not allow spaces in the values then use a space delimited list.

%let dates=31oct2020 31jan2021 31mar2021;

Now if you want to use that list inside a macro definition (where you can have macro logic) then it is simple.  So here is an simple contrived example.  

%macro mymacro(dates);
%local index date dsname ;
%do index=1 %to %sysfunc(countw(&dates,%str( )));
  %let date=%scan(&dates,&index,%str( ));
   %let dsname=new_&date;
   data &dsname;
      set have;
      where datevar = "&date"d ;
   run;
%end;
%mend mymacro;

%mymacro(dates=31oct2020 31jan2021 31mar2021)

View solution in original post

11 REPLIES 11
Reeza
Super User

This is splitting data into subsets. Can you explain why you're doing this? It's typically not recommended.

How are your multiple dates stored? In a macro variable or data set or derived logically (ie current month).

ssafmed
Obsidian | Level 7

I have a code that I want to exceute for 20 dates and then store it in differents 20 output dataset!

 

thanks

acordes
Rhodochrosite | Level 12

use do loop construct and the magical intck and intnx functions for that.

 

like reeza said, it seems that you're trying to create as many data sets as days in the range of the 2 dates. I've interpreted that you'd like to create a series of dates each corresponding to one row. 

 

%let cdate='31oct2020'd;
%put &cdate;a1.png

data temp;
format dat date9.;
do i=1 to intck('days', &cdate, '31jan2021'd) ;
dat=intnx('days', &cdate, i);
output;
end;
run;

 

ssafmed
Obsidian | Level 7

Thanks but I have 20 different dates!

Reeza
Super User

If you can't provide any more information for whatever reason, see this tutorial on how to convert a working program to a macro and run it for a series of parameters stored in a data set. 



Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md


Tom
Super User Tom
Super User

What does that mean?

20 dates from where?  Are you planning to type them in?

data dates;
do date='01JAN2020'd,'05MAR2020'd;
   output;
end;
  format date date9.;
run;

Or read them from some dataset:

proc sql;
create table dates as 
select distinct date 
  from have
;
quit;

Or is there some pattern to the dates so that you can generate 20 new dates from some starting point?

ssafmed
Obsidian | Level 7

Yes but I want the output to be like :

 

new_31oct2020

new_31jan2021

new_31mar2021

....

 

 

 

 

 

Reeza
Super User
Coffee or tea?
Yes.......

There's an OR there - so which is the correct option that Tom presented?
Tom
Super User Tom
Super User

I have lost the thread of what the question is.

 

If you want to do the same thing over a list of values then in MACRO code the easiest way is to put the list of values into a macro variable.  Since your example appear to not allow spaces in the values then use a space delimited list.

%let dates=31oct2020 31jan2021 31mar2021;

Now if you want to use that list inside a macro definition (where you can have macro logic) then it is simple.  So here is an simple contrived example.  

%macro mymacro(dates);
%local index date dsname ;
%do index=1 %to %sysfunc(countw(&dates,%str( )));
  %let date=%scan(&dates,&index,%str( ));
   %let dsname=new_&date;
   data &dsname;
      set have;
      where datevar = "&date"d ;
   run;
%end;
%mend mymacro;

%mymacro(dates=31oct2020 31jan2021 31mar2021)
ssafmed
Obsidian | Level 7
Thanks a lot!
Reeza
Super User
data _null_;
	do i=1 to 20;
		date=intnx('qtr', today(), -i, 'e');
		date_char=put(date, date9.);
		call execute (catt('data new_c', 
		                    date_char, 
		                    '; format dat date9.; dat=', 
			                date, 
			                ';run;')
			          );
	end;
run;

Does last 20 quarters fully dynamically. 

 

OTE: CALL EXECUTE generated line.
 1         + data new_c31DEC2021; format dat date9.; dat=22645;run;
 
 NOTE: The data set WORK.NEW_C31DEC2021 has 1 observations and 1 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds
       memory              516.90k
       OS Memory           25764.00k
       Timestamp           01/21/2022 05:33:54 PM
       Step Count                        63  Switch Count  2
       Page Faults                       0
       Page Reclaims                     94
       Page Swaps                        0
       Voluntary Context Switches        10
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           264
       
 
 2         + data new_c30SEP2021; format dat date9.; dat=22553;run;
 
 NOTE: The data set WORK.NEW_C30SEP2021 has 1 observations and 1 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds
       memory              516.90k
       OS Memory           25764.00k
       Timestamp           01/21/2022 05:33:54 PM
       Step Count                        64  Switch Count  2
       Page Faults                       0
       Page Reclaims                     88
       Page Swaps                        0
       Voluntary Context Switches        10
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           264
       
 
 3         + data new_c30JUN2021; format dat date9.; dat=22461;run;
 
 NOTE: The data set WORK.NEW_C30JUN2021 has 1 observations and 1 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds
       memory              516.90k
       OS Memory           25764.00k
       Timestamp           01/21/2022 05:33:54 PM
       Step Count                        65  Switch Count  2
       Page Faults                       0
       Page Reclaims                     88
       Page Swaps                        0
       Voluntary Context Switches        10
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           264
       
 
 4         + data new_c31MAR2021; format dat date9.; dat=22370;run;
 
 NOTE: The data set WORK.NEW_C31MAR2021 has 1 observations and 1 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds
       memory              516.90k
       OS Memory           25764.00k
       Timestamp           01/21/2022 05:33:54 PM
       Step Count                        66  Switch Count  2
       Page Faults                       0
       Page Reclaims                     88
       Page Swaps                        0
       Voluntary Context Switches        10
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           264
       
 
 5         + data new_c31DEC2020; format dat date9.; dat=22280;run;
 
 NOTE: The data set WORK.NEW_C31DEC2020 has 1 observations and 1 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds
       memory              516.90k
       OS Memory           25764.00k
       Timestamp           01/21/2022 05:33:54 PM
       Step Count                        67  Switch Count  2
       Page Faults                       0
       Page Reclaims                     88
       Page Swaps                        0
       Voluntary Context Switches        10
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           264
       
 
 6         + data new_c30SEP2020; format dat date9.; dat=22188;run;
 

Reeza_0-1642786680312.png

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 11 replies
  • 1150 views
  • 3 likes
  • 4 in conversation