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!
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.
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?
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
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.
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
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!
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
Thank you very much!
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.