BookmarkSubscribeRSS Feed
Karo_22
Calcite | Level 5

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;

4 REPLIES 4
ballardw
Super User

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.


Karo_22
Calcite | Level 5

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.

ballardw
Super User

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

Ksharp
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 759 views
  • 6 likes
  • 3 in conversation