BookmarkSubscribeRSS Feed
SSin
Obsidian | Level 7

I am trying to recode a character variable "Gender" with responses Male, Female and NA. 

I used   

data =  WORK.IMPORT2; 

if 'Gender' = 'Male' or 'Gender' = 'Female' 'Gender' = 'NA' ;
if 'Gender' = 'Male' then male= 1; else if 'Gender' = 'Female' 'NA' then male =0;

 and 

If 'Gender' = 'Male' then male =1; else male=0;
If 'Gender' = 'Female' then female =1; else female=0;

Both of these are not working and giving me same error message "Statement is not valid or it is used out of proper order". Could you please help me how to recode? I attached SAS file. 

Many thanks!

3 REPLIES 3
gamotte
Rhodochrosite | Level 12

Hello,

 

The correct syntax for a data step is :

 

data <output_dataset_name>;

    set <input_dataset_name>;

[...]

 

Also you are testing the equality of strings 'Gender' and 'Male'/'Female' so your tests will always be false.

I think you mean the Gender variable so you have to drop the quotes :

 

if Gender='Male' then ...

Kurt_Bremser
Super User

Don't overcomplicate it, use a select() block:

data have;
input gender $;
datalines;
male
female
NA
;

data want;
set have;
select (gender);
  when ('male') do;
    male = 1;
    female = 0;
  end;
  when ('female') do;
    male = 0;
    female = 1;
  end;
  when ('NA') do;
    male = 0;
    female = 0;
  end;
end;
run;

proc print data=want noobs;
run;

Result:

gender    male    female

male        1        0  
female      0        1  
NA          0        0  
ballardw
Super User

@SSin wrote:

I am trying to recode a character variable "Gender" with responses Male, Female and NA. 

I used   

data =  WORK.IMPORT2; 

if 'Gender' = 'Male' or 'Gender' = 'Female' 'Gender' = 'NA' ;
if 'Gender' = 'Male' then male= 1; else if 'Gender' = 'Female' 'NA' then male =0;

 and 

If 'Gender' = 'Male' then male =1; else male=0;
If 'Gender' = 'Female' then female =1; else female=0;

Both of these are not working and giving me same error message "Statement is not valid or it is used out of proper order". Could you please help me how to recode? I attached SAS file. 

Many thanks!


In none of the code above do you reference a Variable that might contain values of 'Male' or 'Female'. Variables are not placed in quotes when you want to examine or modify the values of the variable. Actually there are very few times a variable name should appear in quotes generally.

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