BookmarkSubscribeRSS Feed
atulsingh
Obsidian | Level 7

I am submitting the below code:

 

%let value = 150;
%macro if_then_else(dataset,var1,var);
data conditional;
set &dataset;run;
%if &var1 > &value %then &var=&var*0.3 + &var;
%else &var = &var;
proc print data=conditional;run;
%mend if_then_else;

 

%if_then_else(matchmerge_mtcars,hp,price);

 

I am ending up with the error: ERROR 180-322: Statement is not valid or it is used out of proper order.

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Show us a representative portion of your data.

 

Show us the SASLOG (with option MPRINT turned on).

--
Paige Miller
atulsingh
Obsidian | Level 7

1111.png

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I repeat again:

2 things, as mentioned before:

- Post test data in the form of a datastep in the post using the {i} code window.

- Macro is not needed.  

Why are you insiting on not using Base SAS?

%let value=150;

data conditional;
  set matchmerge_mtcars;
  hp=ifn(price>&value.,price*0.3,price);
run;

As for your question specifically, its because you don't understand what its doing.  Macro is not Base SAS, it is a find and replace system to generate text.  You %if statement generates a datastep statement, but is not within a datastep.  Really suggest learning Base SAS, then move onto Macro later on as it really doesn't add anything until you know what it is for.

 

Astounding
PROC Star

Here is the code that your macro generates:

 

data conditional;
set matchmerge_mtcars;run;

price=price

proc print data=conditional;run;

 

Do you see anything wrong with it?

 

The real issue is understanding what macro language does, and why this is the resulting program.  Notice:

 

%IF %THEN statements come after the RUN statement.  Whatever they do (and they are not doing the right thing), the result is not part of the DATA step.

 

Even if you were to move the RUN statement, there is no reason to use %IF %THEN in this program.  %IF %THEN generates statements that can become part of the DATA step, but %IF %THEN never never never processes each observation in the data set.  IF THEN would be an acceptable tool (also see the @RW9 suggestion).

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
  • 1035 views
  • 0 likes
  • 4 in conversation