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!
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 ...
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
@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.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.