DATA Step, Macro, Functions and more

sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

Accepted Solution Solved
Reply
Super Contributor
Posts: 345
Accepted Solution

sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

%macro mactem2(tem );
%if &tem ne "low" %then %do;
data hem;
set chicken;
importance2=importance+100;
run;
%end;
%else if &tem ="low" then %do;
importance3=importance+30;
run;
%end;
%mend mactem2;
there is no error message. But I can only find variable importance2 but not importance3.
any advice to modify my code so I can generate the variable importance3? Thanks mactem2(low); mactem2(mid); mactem2(war); run; quit;

Accepted Solutions
Solution
‎06-20-2016 11:25 AM
Super User
Posts: 7,866

Re: sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

[ Edited ]

wenling wrote:

%macro mactem2 (tem );

data hem;

set chicken;

%if &tem ne "low" %then %do;

importanc2=importance+100;

run;

%end;

%else %if &tem = "low" %then %do;

importance3= importance+30;

run;

%end;

%mend mactem2;

%mactem2(low);

%mactem2(mid);

%mactem2(war);

run;

quit;

 

 

still I cannot create the variable importance3. Check the log file, it always believe the statment is True, that is, it believe the statement is true, that is, %if condition &tem ne "low" is True

 

I also tried to replace &tem with tem, as tem is my variable

ANy advice?


This:

%if &tem ne "low" %then %do;

and this:

%else %if &tem = "low" %then %do;

are the culprits.

You once again missed the quotes.

It must look like this:

%if "&tem" ne "low" %then %do;

and this:

%else %if "&tem" = "low" %then %do;

 

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 3,261

Re: sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

[ Edited ]
%macro mactem2(tem );
%if &tem ne low %then %do;
data hem;
set chicken;
importance2=importance+100;
run;
%end;
%else if &tem = low %then %do;
data hem; importance3=importance+30; run; %end; %mend mactem2; mactem2(low); mactem2(mid); mactem2(war);
Super User
Posts: 19,878

Re: sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

[ Edited ]

Importance3 is after the run statement. 

It doesn't end up in a data step so it never gets created. 

 

If you want the data step regardless you don't need a macro %if then. Also all the if/then/do need a % symbol in front of them. You also need a % in front of your macro call. 

 

Does this get you what you want. 

 



%macro mactem2(tem );

data hem;
set chicken;

if "&tem" ne "low" then importance2=importance+100; else if "&tem" ="low" then importance3=importance+30; run; %mend mactem2;
%mactem2(low); %mactem2(mid); %mactem2(war);

 

 

Super User
Posts: 19,878

Re: sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

Also, since your dataset name is not unique when you run the three macro calls one after each other only the final one exists, each one overwrites the previous dataset (hem).

Super Contributor
Posts: 345

Re: sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

Thank you very much.

But I am required to use macro %IF-%THEN/%ELSE Statement,

Super User
Posts: 19,878

Re: sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

Ok. Well the structure is identical. Replace my if statements with macro if then statements. 

Super Contributor
Posts: 345

Re: sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

%macro mactem2 (tem );

data hem;

set chicken;

%if &tem ne "low" %then %do;

importanc2=importance+100;

run;

%end;

%else %if &tem = "low" %then %do;

importance3= importance+30;

run;

%end;

%mend mactem2;

%mactem2(low);

%mactem2(mid);

%mactem2(war);

run;

quit;

 

 

still I cannot create the variable importance3. Check the log file, it always believe the statment is True, that is, it believe the statement is true, that is, %if condition &tem ne "low" is True

 

I also tried to replace &tem with tem, as tem is my variable

ANy advice?

Super User
Posts: 19,878

Re: sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

Your second if/then/do is missing an END?

 

You have an extra END outside of the run?

 

Are there variables in your dataset called low/mid/var? If not either enclose all strings in quotes or none in your comparison. 

 

%if &item = low %then ...

 

Again, if you call them one after each other you won't see interim datasets. I suggest adding a proc print after run but before mend to see the data results. You can print just a few observations. 

Super User
Posts: 19,878

Re: sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

Quick tip, If you indent your code you'll see a lot of these issues right away. 

Solution
‎06-20-2016 11:25 AM
Super User
Posts: 7,866

Re: sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

[ Edited ]

wenling wrote:

%macro mactem2 (tem );

data hem;

set chicken;

%if &tem ne "low" %then %do;

importanc2=importance+100;

run;

%end;

%else %if &tem = "low" %then %do;

importance3= importance+30;

run;

%end;

%mend mactem2;

%mactem2(low);

%mactem2(mid);

%mactem2(war);

run;

quit;

 

 

still I cannot create the variable importance3. Check the log file, it always believe the statment is True, that is, it believe the statement is true, that is, %if condition &tem ne "low" is True

 

I also tried to replace &tem with tem, as tem is my variable

ANy advice?


This:

%if &tem ne "low" %then %do;

and this:

%else %if &tem = "low" %then %do;

are the culprits.

You once again missed the quotes.

It must look like this:

%if "&tem" ne "low" %then %do;

and this:

%else %if "&tem" = "low" %then %do;

 

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 345

Re: sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

Posted in reply to KurtBremser
Thank you very much!!!
Super User
Super User
Posts: 7,997

Re: sas macro %IF-%THEN/%ELSE Statement, why cannot I generate a second variable

I have to ask, why?  The reason is that macro language has its purposes but trying to force simple code through it will create unreadable, unmaintable code.  Its quite a simple concept, macro is to generate code which is repeated - to avoid unessecary repeating, it is not there as a replacement for Base SAS which is the programming language.  Your example code basically comes down to this:

data hem;
  set chicken;
  importance2=ifn("&TEM.",importance+100,importance+30);
run;

So if you really need that in a macro (and if I saw that in a macro I would be hitting delete straight away):

%macro Tmp (tem=);
  data hem;
    set chicken;
    importance2=ifn("&TEM.",importance+100,importance+30);
  run;
%mend Tmp;

You will note some simple things to make the code more readable - indentation for instance, simplfying the code to its necessary parts, finishing macro variables with ".".

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 486 views
  • 1 like
  • 5 in conversation