BookmarkSubscribeRSS Feed
cwpark
Calcite | Level 5

Hello,

 

I'm trying to use macro %if %then; %else; for a numeric operand, and I'd appreciate if anyone could let me know how to do it. I would like to create a new variable named "status" to indicate whether it is "serious" or "moderate."

 

%macro Serious_Smoker(serious) /store;  

   %let Serious_Smoker = %upcase(&serious);

   %if &PctSmokers > 35 %then

       %do;

          title 'Results for Serious Smoker Population (>35%)';

          Status = "Serious";

run;

   %end;

   %else

       %do;

          title 'Results for Moderate Smoker Population (<=35%)';

          Status = "Moderate";

run;

%end;

%mend Serious_Smoker;

%Serious_Smoker;

 

I got error messages: 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric

       operand is required. The condition was: &PctSmokers > 35

ERROR: The macro SERIOUS_SMOKER will stop executing.

 

--

 

Thank you very much for your help in advance!

8 REPLIES 8
Astounding
PROC Star

The macro language processor applies %EVAL to all %IF conditions.  In this case, I would guess that the value for &PctSmokers contains a decimal point, and %EVAL can't handle a decimal point.  But a slightly different function can.  Switch to:

 

%if %sysevalf(&PctSmokers > 35) %then

       %do;

 

The parentheses really are in the proper place here.

cwpark
Calcite | Level 5

Thank you for your reply. PctSmokers are continuous variable that has no decimal points. 

 

The number of dataset that I input are three: PctSmokers (continuous), income (categorical), and age (categorical). 

 

In addition to that, I would like to add "Status" variable in the macro "if then" statements. I think I need a numeric operand for local macro variable "serious" and also need to figure out how to create "Status" variable. 

 

How do I seprately display >35% and <=35% smoker population? 

art297
Opal | Level 21

I have a number of questions.

 

Your macro only has one parameter (i.e., serious), but when you call the macro you don't provide a value for the macro variable SERIOUS.

 

Then, in your macro, you check to see if &PctSmokers is greater than 35. Where did you declare that macro variable?

 

Third, you have a line of code that sets the value of a non-macro variable called SERIOUS. That code would only work if it is called from within a data step.

 

Art, CEO, AnalystFinder.com

 

cwpark
Calcite | Level 5

Thanks for your help!

 

I set 'serious' as a local macro variable. There are three variable in the original dataset: PctSmokers, income, and age. Depending on PctSmokers, I'd want to see two divided results: serious smoker population vs moderate smoker population, by using %if %then macro statement.

 

I think I need to use a function for numeric operand, not "upcase," and I'm figuring out what it is. 

art297
Opal | Level 21

It would help if you provide some example datasets, all of your code, and a brief explanation of what you are trying to do. A macro may not even be needed.

 

Art, CEO, AnalystFinder.com

 

cwpark
Calcite | Level 5

The dataset example is: 

 

data smoker;
input PctSmokers Income $ Age $; 
datalines; 
45 1 1
35 2 2
14 5 3
27 1 1
20 2 2

 

I'd want to see two different results: Results for Serious Smoker Population (>35%) and Results for Moderate Smoker Population (<=35%), by adding "Status" variable saying "Serious" for >35% and "Moderate" for <=35%. 

 

I know I don't need to use macro, but I'd like to approach more complicated dataset soon by knowing how to use macro with if then statement. Thanks! 

art297
Opal | Level 21

I agree with the importance of learning how to write macros, but I'm not sure how a macro could help you here. Here are two non-macro approaches:

data smoker;
input PctSmokers Income $ Age $; 
datalines; 
45 1 1
35 2 2
14 5 3
27 1 1
20 2 2
;

data want;
  set smoker;
  length Status $8;
  if PctSmokers>35 then Status="Serious";
  else if 0<=PctSmokers<=35 then Status="Moderate";
run;

proc format;
  value status
  low-35='Moderate'
  36-High='Serious'
  ;
run;

proc print data=smoker;
  format PctSmokers status.;
run;

Art, CEO, AnalystFinder.com

 

cwpark
Calcite | Level 5

Thank you very much! Smiley Very Happy

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
  • 8 replies
  • 1308 views
  • 4 likes
  • 3 in conversation