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

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 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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;

 

View solution in original post

6 REPLIES 6
ChrisNZ
Tourmaline | Level 20

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;

 

aowais
Obsidian | Level 7

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...

ballardw
Super User

@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?

ChrisNZ
Tourmaline | Level 20

> 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;

 

aowais
Obsidian | Level 7

Thank you! This worked! 

ballardw
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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