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!
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;
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.