- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you! that works
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.