I have about 29 character variables in a dataset that need to be changed depending on which column it is in. I know how to list all the IF statement like below, but would like to do it different as it will help in similar projects in the future. So if the values are not equal to zero then I need to change the values depending on which column it is in.
for example
DATA HAVE;
LENGTH F1 $2 F2 $2;
INPUT F1 $ F2 $
DATALINES;
00 00
10 12
00 12
10 00
;
RUN;
DATA WANT;
SET HAVE;
IF FR1 ^= '00' THEN FR1 = '05';
IF FR2 ^= '00' THEN FR2 = '36';
RUN;
Like this?
data WANT;
set HAVE;
array NEW [2] $2 _temporary_ ('05','36');
array COL [2] F1-F2;
do i =1 to 2;
if COL[I]='00' then COL[I]=NEW[I];
end;
run;
Like this?
data WANT;
set HAVE;
array NEW [2] $2 _temporary_ ('05','36');
array COL [2] F1-F2;
do i =1 to 2;
if COL[I]='00' then COL[I]=NEW[I];
end;
run;
Thank you! that works
Another approach is to move the If/then logic to a format.
Proc format library=work; value $F1_ '00' = '00' other= '05' ; value $F2_ '00' = '00' other= '36' ; DATA HAVE; LENGTH F1 $2 F2 $2; INPUT F1 $ F2 $ ; DATALINES; 00 00 10 12 00 12 10 00 ; RUN; DATA WANT; SET HAVE; f1 = put(f1,$f1_.); f2 = put(f2,$f2_.); RUN; /* or use the format without changing values*/ Proc print data=have; format f1 $f1_. f2 $f2_.; run;
Format names can't end in a digit so I used the _.
This has some flexibility because you said "it different as it will help in similar projects in the future.".
The values assigned by a format can be used by almost any analysis, graphing or reporting procedure. So if you have multiple values that need the '00' or '36' coding you don't really need to modify the data sets but can just apply the format when needed.
Or change the coding for any one variable to pick different coding as needed. Suppose your F1 variable would in some case (given more values than '00' and '10' ) like to have 3 report groups. If you actually change the value then you would have to retrace the to an earlier form of the data set before doing the recoding. But if you leave the original values alone then using a different format on the original value uses the variable as desired.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.