BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
A_Halps
Obsidian | Level 7

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');  
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

4 REPLIES 4
Reeza
Super User
Do you have a data set that has the old and new names? If you do, or can create that one, I have a macro you can use here, it's super efficient because it uses PROC DATASETS. Your code above doesn't work, because each time you're starting from severity_lrd_v3b which does not have the previous runs changes so you'll only see the results of the last macro call.
A_Halps
Obsidian | Level 7
I only have a dataset with the q7_16 variables. Not one with the actual words
Reeza
Super User
Can you make one? Seems easier than macro calls but ultimately your choice.

If you want to fix your macro above you just need to change the data set name as I mentioned previously so it's re-using the same data set each time so it adds to it instead of using the original data set.

Tom
Super User Tom
Super User

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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 642 views
  • 2 likes
  • 3 in conversation