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

Hi,

 

I'd like to replace values of a list of similar variables (replace 'C' with '2') for 12 variables:

data want;
   set dat;
  if var01 = 'C' then var01 = '2';
  if var02 = 'C' then var02 = '2';
  if var03 = 'C' then var03 = '2';
  ...
  if var12 = 'C' then var12 = '2';
run;

How can I simplify this code with macro/loop? I tried the following codes but did not work:

%let var_list = var01-var12;
%macro loop(var_list);
%local i next_var;
data want;
   set dat;
   %do i = 1 %to %sysfunc(countw(&var_list));
   %let next_name = %scan(&var_list, &i);
   if &next_var = 'C' then &next_var = '2';
   %end;
run;
%mend;
%loop(&var_list); 

Any idea? Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

No macro needed. Use an ARRAY.

 

data want;
    set dat;
    array v var01-var12;
    do i=1 to dim(v);
        if v(i)='C' then v(i)='2';
     end;
    drop i;
run;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

No macro needed. Use an ARRAY.

 

data want;
    set dat;
    array v var01-var12;
    do i=1 to dim(v);
        if v(i)='C' then v(i)='2';
     end;
    drop i;
run;
--
Paige Miller
panda
Quartz | Level 8
Thanks it worked!
Reeza
Super User
How complicated does this get? Would a format be appropriate as well - do you need actual recoding to numeric values as well? Or will a character variable be sufficient?
panda
Quartz | Level 8
a character variable is sufficient.

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!

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