Hi,
I am trying to recode a number of variables with three categories 1=Yes, 2=No and 3=N/A as 1=Yes, 0=No and 3=. (i.e. missing). I've tried using the following code, but this changes both 2 and 3 to 0.
ARRAY VARS{5} g3_1 g3_2 g3_3 g3_4 g3_5;
DO _N_ = 1 TO 5;
IF VARS{_N_} NE 1 THEN VARS{_N_} = 0;
END;
Help please. Thanks!
1. Do not write all upper case code, that's hard to read
2. This might help:
do I = 1 to 5;
if VARS{I} =2 then VARS{I} = 0;
if VARS{I} =3 then VARS{I} = .;
end;
1. Do not write all upper case code, that's hard to read
2. This might help:
do I = 1 to 5;
if VARS{I} =2 then VARS{I} = 0;
if VARS{I} =3 then VARS{I} = .;
end;
Thank you. Point noted about upper case.
When I try your suggestion, it works for the first two variables, but for the last three, all values end up missing...
@aowais wrote:
Thank you. Point noted about upper case.
When I try your suggestion, it works for the first two variables, but for the last three, all values end up missing...
Example input and output data. This sounds like a data issue and you have not shared any actual values.
Also show your code. A common mistake is to use the code structure of
data have;
set have;
<code that modifies values>
run;
The first pass through the data means that you have replaced the original values and are now further modifying the incorrectly coded values from the first pass through the data.
Does your log show any warnings?
> it works for the first two variables, but for the last three, all values end up missing...
If the values are the same and the logic is the same, then the result is the same.
Check your data.
A more flexible logic might also help:
do I = 1 to 5;
if VARS{I} = 1 then ;
else if VARS{I} = 2 then VARS{I} = 0;
else VARS{I} = .;
end;
Thank you! This worked!
I have to assume that you mean you want to change the value of the variable from 2 to 0 and 3 to missing. Your code shows nothing about yes. no or n/a. If you have format that displays that text for the values you will need a different format.
data want; set have; ARRAY VARS{5} g3_1 g3_2 g3_3 g3_4 g3_5; DO _N_ = 1 TO dim(vars); if vars[i]=2 then vars[i]=0; else if vars[i]=3 then vars[i]=.; END ; run;
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!
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.