DATA Step, Macro, Functions and more

if else macro loop

Reply
Regular Contributor
Posts: 168

if else macro loop

Hi All,

 

I have longitudnal data and grp variable and fl variable, values of grp as 1 2 3 4 5 6... and values for fl as 1 2 3... need to create variable net.

Is there any dynamic way to get the grp and fl values

 

if grp=1 then do; if fl=1 then net=1; if fl=2 then net=2;end;
if grp=2 then do; if fl=1 then net=3; if fl=2 then net=4;end;
if grp=3 then do; if fl=1 then net=5; if fl=2 then net=6;end;
if grp=4 then do; if fl=1 then net=7; if fl=2 then net=8;end;

 

 

my try :

%let maxgrp=4;%let fl=2;

proc sql;

 select *,

%do i=1 %to &maxgrp;  j=1 %to &fl;

     case when grp=&i and fl eq &j then &i*&j;

    end as trt

from ids;

quit;

 

 

Thanks

Sam

Super User
Posts: 19,822

Re: if else macro loop

You can simplify it to a formula:

net=(2*(grp-1))+fl;
Regular Contributor
Posts: 168

Re: if else macro loop

Thank you Reeza!!!

can we adopt this formula under the sql? if i used in data step i got the output , but if i want to adopt for sql, getting warning on log,

"net variable already exit in XXXX dataset"

 

 

 

Thanks

Sam

Super User
Posts: 19,822

Re: if else macro loop

In SQL it would work as well but it would be
(2*(grp-1))+fl as net

If the NET variable already exists in the dataset, as your warning indicates that something to be cautious of, do you really want to overwrite the other variable called net. In a data step it would overwrite it without any warning.
Ask a Question
Discussion stats
  • 3 replies
  • 243 views
  • 2 likes
  • 2 in conversation