BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bal23
Lapis Lazuli | Level 10
%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;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@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;

 

 

View solution in original post

11 REPLIES 11
SASKiwi
PROC Star
%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);
Reeza
Super User

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

 

 

Reeza
Super User

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

Bal23
Lapis Lazuli | Level 10

Thank you very much.

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

Reeza
Super User

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

Bal23
Lapis Lazuli | Level 10

%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?

Reeza
Super User

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. 

Reeza
Super User

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

Kurt_Bremser
Super User

@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;

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

 

 

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
  • 11 replies
  • 10407 views
  • 1 like
  • 5 in conversation