%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;
@Bal23 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;
%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);
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);
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).
Thank you very much.
But I am required to use macro %IF-%THEN/%ELSE Statement,
Ok. Well the structure is identical. Replace my if statements with macro if then statements.
%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?
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.
Quick tip, If you indent your code you'll see a lot of these issues right away.
@Bal23 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;
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 ".".
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.