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

Hello!

I am sorta new to SAS and I was wondering it is possible to have multiple variables assigned to one macro in this fashion:

 

%let var= covid_worried covid_isolated covid_disruptive covid_control confidante es_close
es_friends es_group ECOG_CONCERNED_THINKING;
%let var1= HOUSEHOLD_ALONE CAREGIVERS
children_see children_talk children_close marital_satisfied children_satisfaction;
%let var2= W2_SES_LADDER_CHILDHOOD W4_SES_LADDER_CHILDHOOD;
%let var3= marital;


data long1;
set long;
if &var= '99' or &var= '88' or &var= '77' then &var= '.';
if &var1= '99' or &var1= '88' or &var1= '77' or &var1= '111' then &var1= '.';
if &var2= '99' or &var2= '88' or &var2= '77' or &var2= '101' then &var2= '.';
if &var3= '99' or &var3= '88' or &var3= '7' then &var3= '.';
run;

 

I grouped the macros based on variables that had similar formatting. However, this code did not work. Thank you all in advance for your help!!

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Those you have multiple alphanumeric variable which you, depending on their value, want to set to ".". This seems strange: Why not setting them to missing? Instead of using macro variable, you could use arrays to group the variable you have to compare with the same values. And, yes, you have to check each variable.

Some ideas to simplify the matter:

Replacing if &var= '99' or &var= '88' or &var= '77' then &var= '.';

with

array group1[6] covid_worried covid_isolated covid_disruptive covid_control confidante es_close;

do i = 1 to dim(group1);
  if group1[i] in ("99", "88", "77") then group1[i] = ".";
end;

should work.

 

View solution in original post

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

The macro language is mostly a text generator.

Your code becomes:

data long1;
set long;
if covid_worried covid_isolated covid_disruptive covid_control confidante es_close
es_friends es_group ECOG_CONCERNED_THINKING= '99'
or covid_worried covid_isolated covid_disruptive covid_control confidante es_close
es_friends es_group ECOG_CONCERNED_THINKING= '88' or
covid_worried covid_isolated covid_disruptive covid_control confidante es_close
es_friends es_group ECOG_CONCERNED_THINKING= '77'
then covid_worried covid_isolated covid_disruptive covid_control confidante es_close
es_friends es_group ECOG_CONCERNED_THINKING= '.';

which is obviously invalid syntax.

Before using macros, familiarise yourself with arrays. Post you code here is you have issues.

Use the appropriate icon to post code please.

 

andreas_lds
Jade | Level 19

Those you have multiple alphanumeric variable which you, depending on their value, want to set to ".". This seems strange: Why not setting them to missing? Instead of using macro variable, you could use arrays to group the variable you have to compare with the same values. And, yes, you have to check each variable.

Some ideas to simplify the matter:

Replacing if &var= '99' or &var= '88' or &var= '77' then &var= '.';

with

array group1[6] covid_worried covid_isolated covid_disruptive covid_control confidante es_close;

do i = 1 to dim(group1);
  if group1[i] in ("99", "88", "77") then group1[i] = ".";
end;

should work.

 

Tom
Super User Tom
Super User

Are the variables character or numeric?  If character why change the value to a period? Why not just a space?  Or better still why change the value at all?

 

But it looks like you are dealing with a numeric variable where they used large numbers like 99 to mean some type of missing value. Since you have multiple codes you probably should use special missing values for some of them so you can continue to distinguish between them.

 

The macro variables are not really doing much for you here, unless for some reason you need to use that list of variable names in multiple places in your code.  Also you said the lists used different codes, but they seem to all be using the same set of missing value codes.

Let's assume that 99 is the actually missing value and that 88 and 77 are special missing codes (for example "not applicable") so let's recode those to .A and .B instead of normal missing.

data want;
  set have;
  array recode
    covid_worried covid_isolated covid_disruptive covid_control confidante es_close
    es_friends es_group ECOG_CONCERNED_THINKING
    HOUSEHOLD_ALONE CAREGIVERS
    children_see children_talk children_close marital_satisfied children_satisfaction
    W2_SES_LADDER_CHILDHOOD W4_SES_LADDER_CHILDHOOD
    marital
  ;
  do index=1 to dim(recode);
     if recode[index]=99 then recode[index]=.;
     else if recode[index]=88 then recode[index]=.A ;
     else if recode[index]=77 then recode[index]=.B ;
  end;
  drop index ;
run;

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 3037 views
  • 3 likes
  • 4 in conversation