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

This may be a very basic question about macro, how could I make a macro code repeated conditions and table names?

If someone can tell me how to solve it, I would really appreciate it. Please!

 

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

12 REPLIES 12
novinosrin
Tourmaline | Level 20

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

JayChoi
Fluorite | Level 6

 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.

 

Here, cnt=1 means one quarter, and one dataset contains 16 quarters.
 
Could you explain me more?
 
Thank you!
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
JayChoi
Fluorite | Level 6

I really thank you for your answer.

 

I would like to extract a separate data set for each 16 quarters (cnt=1 means one quarter) from a large data set (xxxx), calculate the estimates through regression analysis with each sub data set, and then combine them into one large data set.
In other words, I need to create separate data sets, such as cnt 1 through 16 (from cnt=1 to 16), cnt 2 through 16 (from cnt=2 to 17), etc.
 
Could you help me more?
 
Thank you!
PaigeMiller
Diamond | Level 26

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.

 
 
--
Paige Miller
JayChoi
Fluorite | Level 6
Thank you for your answer. I mean I have to make sub data sets from a large data set "xxxx". 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.
PaigeMiller
Diamond | Level 26

@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.

 
 
--
Paige Miller
JayChoi
Fluorite | Level 6
 
novinosrin
Tourmaline | Level 20

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.

Astounding
PROC Star

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.

JayChoi
Fluorite | Level 6

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!

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!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 12 replies
  • 826 views
  • 6 likes
  • 4 in conversation