SAS Macro do loop

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

SAS Macro do loop

Hi, I have this problem I am hoping you guys can help me with,

 

My data looks like this

group score

 1        100

 2         200;

 

so I want somethign like this, I want to expand this data so that for each group teh values go from 0 to the value in column score. something like this,  - 101(0 to 100)+201 (0 to 200)=302 number of observations.

gropu   increment

1           0

1           1

1            2

.

1          100

2            0

2            1

2             2

.               .

2            200.

(note "." does not mean missing it means contined) so total number of ovservations isoup  inc_score.)

 

I did somethign like this and it works fine -

 

data x2; set x;

by group;

if first.group then do increment=0 to score by 1;

output;

end;

drop score;

run;

 

Now I want to do it in a macro step. since my score column will vary depending on different dataset. But I am unable to iterate thourgh each group.

So far I tried somethiing like this.

 

%macro eg;

data x2;  set x ;
by group;

if first.group then call symput('var',score);
increment=0;
%do  i=0 %to &var;
increment+1;
output;
%end ;
%mend;
%eg ; run;

 

The "by 1" option I used with do loop wihtout macro does not work in macro. I tried many versions,  but the macro is always giving me 201+201=402 observatiosn. That is, it is not using the fact taht group 1 only should go from 0 to 100, rather it is doing 0 to 200 for both group. Anyhelp will be highly appreciated. I spent a lot of time on this, but could not resolve it yet.

 

Thank you.

 

 


Accepted Solutions
Solution
‎01-19-2016 10:49 AM
Super User
Posts: 11,134

Re: SAS Macro do loop

If you are trying to replace the SCORE variable with a different one, which sort of makes sense then something like this:

%macro eg (var=);

data x2_&var;  /* to get a new set for each variable*/
   set x ;
   by group;

   if first.group then do increment=0 to &var by 1;
      output;
   end;
run;   
%mend;
%eg (var=score); 

Common misuderstanding between maco language and datastep. You only needed to replace the single variable to duplicate the behavior with a different variable. If you wan't to try to increment multiple variables at one pass through the data you would need significanlty different programming as well as defining what your output set would look like with different upperbounds for different variables in the same base record.

 

View solution in original post


All Replies
Solution
‎01-19-2016 10:49 AM
Super User
Posts: 11,134

Re: SAS Macro do loop

If you are trying to replace the SCORE variable with a different one, which sort of makes sense then something like this:

%macro eg (var=);

data x2_&var;  /* to get a new set for each variable*/
   set x ;
   by group;

   if first.group then do increment=0 to &var by 1;
      output;
   end;
run;   
%mend;
%eg (var=score); 

Common misuderstanding between maco language and datastep. You only needed to replace the single variable to duplicate the behavior with a different variable. If you wan't to try to increment multiple variables at one pass through the data you would need significanlty different programming as well as defining what your output set would look like with different upperbounds for different variables in the same base record.

 

Contributor
Posts: 39

Re: SAS Macro do loop

[ Edited ]

Ok at first attempt it did not work because I thought all do loops under macro language should be %do .. %to .. %end. But now I see that it does not work that way. When I tried the code exactly the way you suggested, it worked. I see that a normal do loop withouth % works differenlty under macro.

 

 

Thank you.

 

 

Contributor
Posts: 62

Re: SAS Macro do loop


data test ;
input group score;
cards;
1 10
2 5
3 7
;run;
proc print;run;
data test1;
set test;
score1= 0;
do i =1 to score;
score1+1;
output;
drop score i;
rename score1=score;
end;
run;

it will work fine.

Contributor
Posts: 39

Re: SAS Macro do loop

[ Edited ]

This method would not work because I dont know how the score will vary for different groups. If I am manually inputting it then there is no use of the macro. Infact, I started assuming I know the scores and then wrote the code similar to what you suggested. But now the challenge is to adapt it to a macro where the code will read the score for each group and then increment it. Thanks for your suggestion though.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 439 views
  • 1 like
  • 3 in conversation