BookmarkSubscribeRSS Feed
Daily1
Quartz | Level 8

I have this Query 

%let size=2;
%let Horsepower=132;
Data wanted;
    Set SASHELP.CARS;
	where EngineSize =&size  ;
Run;
data MAPPING;
set wanted;
 %if  EngineSize =2 %then %do;
        where Horsepower= &Horsepower
        %end;
run;

i want if only EngineSize =2 then read where Horsepower= &Horsepower otherwise no

2 REPLIES 2
PaigeMiller
Diamond | Level 26
data MAPPING;
set wanted;
if EngineSize=2 and horsepower=&horsepower;
run;

 

Note: This is not a macro command, this is a plain old data set IF statement, not a macro %IF

 

But even simpler is this:

 

data MAPPING;
set wanted;
where horsepower=&horsepower;
run;

and this works because previously you have limited the observations in WANTED to be only those where EngineSize=2

 

But to continue to simplify ... why do you need two DATA steps for this? One DATA step will do

 

data wanted;
    set SASHELP.CARS;
    where EngineSize=&size and horsepower=&horsepower ;
run;

 

But even that much isn't always necessary, you can do things like this:

 

 

proc whatever data=sashelp.cars(where=(enginesize=&size and horsepower=&horsepower));

 

 

ADVICE: Always — that's ALWAYS — write working code without macros and without macro variables first. Then converting to code with macros and macro variables is always simpler. If your code does not work without macros and without macro variables, it will NEVER work with macros and with macro variables.

--
Paige Miller
Tom
Super User Tom
Super User

Can you provide a description of what you are trying to do?

 

This statement makes no sense.

 %if  EngineSize =2 %then %do;

because it can never be true.  The digit 2 is never equal to a string that starts with an upper case E.

 

If you wanted to test if the macro variable SIZE was 2 you could have done.

data MAPPING;
  set wanted;
%if &size=2 %then %do;
  where Horsepower= &Horsepower;
%end;
run;

If you wanted to test if the value of the variable named ENGINESIZE is 2 then you will need to use SAS code and not MACRO code.  So perhaps you wanted something like:

data MAPPING;
  set wanted;
  if enginesize=2 then do;
    if Horsepower= &Horsepower;
  end;
run;

Note that are WHERE statement cannot execute conditionally so had to switch to a subsetting IF statement instead.

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 652 views
  • 0 likes
  • 3 in conversation