I am trying to change the names of certain variables with a macro because I have many different variables.
So, I need q7_16 = Mild, q7_17 = Moderate, q7_18 = Severe, q7_19 = As_bad, q7_2094 = No_wheeze, and q7_2095 = Wheeze_whistle.
This is what I have tried, but I can't seem to change all of the variables. It will only change the last one.
%macro question (ans_num, answer, label);
proc sql;
create table severity_lrd_v3b_names as
select *,
q7_&ans_num as &answer label &label
from severity_lrd_v3b;
quit;
%mend;
%question (16, Mild, 'Mild');
%question (17, Moderate, 'Moderate');
%question (18, Severe, 'Severe');
%question (19, As_bad, 'As_bad');
%question (2094,No_wheeze, 'No_wheeze');
%question (19, Wheeze_whistle, 'Wheeze_whistle');
Only the last call has any effect because you keep re-reading the original dataset.
The easiest change might be to make the macro just generate part of the SQL code.
%macro question (ans_num, answer, label);
q7_&ans_num as &answer label &label
%mend;
proc sql;
create table severity_lrd_v3b_names as
select *
,%question (16, Mild, 'Mild')
,%question (17, Moderate, 'Moderate')
,%question (18, Severe, 'Severe')
,%question (19, As_bad, 'As_bad')
,%question (2094,No_wheeze, 'No_wheeze')
,%question (19, Wheeze_whistle, 'Wheeze_whistle')
from severity_lrd_v3b
;
quit;
But of course at that point you should recognize that you don't need the macro at all as it is just as easy to type the actual code as the macro calls.
Only the last call has any effect because you keep re-reading the original dataset.
The easiest change might be to make the macro just generate part of the SQL code.
%macro question (ans_num, answer, label);
q7_&ans_num as &answer label &label
%mend;
proc sql;
create table severity_lrd_v3b_names as
select *
,%question (16, Mild, 'Mild')
,%question (17, Moderate, 'Moderate')
,%question (18, Severe, 'Severe')
,%question (19, As_bad, 'As_bad')
,%question (2094,No_wheeze, 'No_wheeze')
,%question (19, Wheeze_whistle, 'Wheeze_whistle')
from severity_lrd_v3b
;
quit;
But of course at that point you should recognize that you don't need the macro at all as it is just as easy to type the actual code as the macro calls.
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!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.