DATA Step, Macro, Functions and more

problem with sas code

Reply
Contributor
Posts: 38

problem with sas code

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.

Trusted Advisor
Posts: 1,909

Re: problem with sas code

Posted in reply to atulsingh

Show us a representative portion of your data.

 

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

Contributor
Posts: 38

Re: problem with sas code

Posted in reply to PaigeMiller

1111.png

Super User
Super User
Posts: 7,942

Re: problem with sas code

Posted in reply to atulsingh

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.

 

Super User
Posts: 5,497

Re: problem with sas code

Posted in reply to atulsingh

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).

Ask a Question
Discussion stats
  • 4 replies
  • 125 views
  • 0 likes
  • 4 in conversation