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.

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 311 views
  • 0 likes
  • 3 in conversation