DATA Step, Macro, Functions and more

Create if/then/do/else macro to complete one of the data steps

Reply
Occasional Contributor
Posts: 16

Create if/then/do/else macro to complete one of the data steps

How do I create if/do/else logic (I guess using macros) where I am trying to do following. I have only one table (mbr2) where there might be state = MA or other states. In case if line has that MA & product 203 then I want to sort by state and product and eff field in that data set, however if line does not have that criteria then just sort by state and eff. All of this at the end would be in one big data set mbr2

if state=MA and product=203 then do

proc sort data=mbr2;by state product eff ;run;

else do

proc sort data=mbr2;by state eff;run;

end;

Super User
Posts: 11,343

Re: Create if/then/do/else macro to complete one of the data steps

If I understand what you want you do not need a macro but you need to subset the data twice and then combine.

Proc sort data=mbr2 (where=(state='MA' and product=203)) out=mbrMA203; by state product eff; run;

Proc sort data=mbr2 (where=(state ne 'MA' or product ne 203)) out=mbrOther; by state  eff; run;

data mbrcombine;

     set mbrMA203 mbrOther;

run;

This will not be sorted by state though. I suspect you are envisioning the MA and 203 into a specific order within the combined data and you have not provided enough information as to where that would be.


Occasional Contributor
Posts: 16

Re: Create if/then/do/else macro to complete one of the data steps

Thanks. I thought of doing a separate data step as well and then combine it. For some reason I was stuck in the if/then/else world.

This will work most likely. Thanks for your feedback.

Super User
Posts: 11,343

Re: Create if/then/do/else macro to complete one of the data steps

Another approach would have been to make an additional variable with the desired sort characteristics, probably based on STATE and eff and product.

Super User
Posts: 10,028

Re: Create if/then/do/else macro to complete one of the data steps

Or you can make a flag variable to avoid spliting the dataset .

data have;

set have;

flag=ifn(product=203,1,0 );

run;

proc sort data=mbr2;by state flag eff ;run;

Or in one sql statement.

proc sql;

create table want as

   select *

    from have

     order by state,(product=203),eff;

quit;

Ksharp

Ask a Question
Discussion stats
  • 4 replies
  • 196 views
  • 6 likes
  • 3 in conversation