- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have a code that I want to exceute for 20 dates and then store it in differents 20 output dataset!
thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
data temp;
format dat date9.;
do i=1 to intck('days', &cdate, '31jan2021'd) ;
dat=intnx('days', &cdate, i);
output;
end;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks but I have 20 different dates!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes but I want the output to be like :
new_31oct2020
new_31jan2021
new_31mar2021
....
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes.......
There's an OR there - so which is the correct option that Tom presented?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;