This may be a very basic question about macro, how could I make a macro code repeated conditions and table names?
The original code is below;
data c1 c2 c3 …. c57; set xxxx;
if cnt=>1 and cnt=<16 then output c1;
if cnt=>2 and cnt<=17 then output c2;
if cnt=>3 and cnt<=18 then output c3;
....
....
....
if cnt=>57 and cnt<=72 then output c57;
run;
A number of posters have already told you that you're heading in a poor direction by trying to create so many subsets. But the solutions they offer aren't all that helpful. Here's a reasonable approach that you could use:
%macro test;
%local i;
%do i=1 %to 57;
data temp /view = temp;
set xxxx;
where &i <= cnt <= &i + 16;
run;
proc reg data=temp;
/* the rest of the code goes here, */
/* which might include more than */
/* proc reg */
run;
%end;
%mend test;
%test
This way, you can properly subset the observations for each iteration, based on your original data set. But you never have to create any subsets.
If you need to use a subset more than once (such as for a series of PROC steps), skip creating a view. Just create the data set itself, as a temporary data set, with the same name each time. Any output data sets that you want to create and save can take on different names by making &I part of the name.
Hi @JayChoi Something like this?
%macro t;
%do i=1 %to 57;
if cnt=>&i and cnt=<%eval(&i+15) then output c&i;
%end;
%mend t;
%t
You can call the macro using %t in the datastep
I really thank you. But all data sets (c1, c2, c3...….c57) have to be extracted for each condition (if cnt=>1 and cnt=<16 then output c1;
if cnt=>2 and cnt<=17 then output c2;...….if cnt=>57 and cnt<=72 then output c57;) from "xxxx" data set.
Since you are new to SAS, maybe you should start by explaining what you are trying to do, the big picture and not the writing of a macro.
What are you going to do with 57 different data sets? Why not one large data set, with some sort of ID value so you can analyze everything at once with BY group processing.
I really thank you for your answer.
This is exactly why you keep everything in one data set, and use BY group processing on the one data set, to perform all these 57 regressions with a very small amount of code, and then the results would be out into a single data set.
@JayChoi wrote:
I mean I have to make sub data sets from a large data set "xxxx".
No, you don't have to make sub-data sets. This is inefficient and makes your programming harder.
The data set xxxx has various variables as well as a quarter variable.
So, like the inefficient code I showed you earlier, I'd like to create efficient macro code that creates each data set.
Macros are completely the wrong tool here.
The general outline of what you want to do is have one data set, where each record has an index indicating which of the 57 groups it belongs to (and possibly it can belong to more than one group), then your run your modeling PROC one time, with a BY statement, and then all the results are output into one data set.
It sounds like you are trying to perform rolling regressions on your 57 time periods. If you search the internet you will find SAS code to perform rolling regressions, you don't have to write your own regression code for 57 data sets.
Hi @JayChoi Please post a brief description of the bigger picture/objective i.e.
1. Please post a sample of your input dataset (mock sample will do)
2. Please post a sample of your expected output for the input sample
3. Please post the business dervation logic to arrive at the expected output
A complete and comprehensive information would enable the community to provide you with a complete solution.
A number of posters have already told you that you're heading in a poor direction by trying to create so many subsets. But the solutions they offer aren't all that helpful. Here's a reasonable approach that you could use:
%macro test;
%local i;
%do i=1 %to 57;
data temp /view = temp;
set xxxx;
where &i <= cnt <= &i + 16;
run;
proc reg data=temp;
/* the rest of the code goes here, */
/* which might include more than */
/* proc reg */
run;
%end;
%mend test;
%test
This way, you can properly subset the observations for each iteration, based on your original data set. But you never have to create any subsets.
If you need to use a subset more than once (such as for a series of PROC steps), skip creating a view. Just create the data set itself, as a temporary data set, with the same name each time. Any output data sets that you want to create and save can take on different names by making &I part of the name.
Rolling regressions in SAS without macros
http://pages.stern.nyu.edu/~adesouza/sasfinphd/index/node25.html
https://communities.sas.com/t5/SAS-Procedures/rolling-window-by-quarter/m-p/343747#M63427
https://communities.sas.com/t5/SAS-Procedures/help-on-rolling-regressions-needed/m-p/46964#M12554
Now I can understand what you said fully. You explained more than what I asked. I mean you taught me until the last step of my works.
I really appreciate you. If you are ok, I'd like to ask you many things. Thank you!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.