in my data, the gender has values like Male, M, m, male, Female, FEMALE, F, f.
When i use proc freq, they are all listed as different values.
I can recode them using if condition, but is there a simpler way to recode them unifomly?
Thanks
Another possibility: don't recode. Create a format to translate:
proc format;
value $gender 'M', 'm', 'Male', 'male' = 'Male'
'F', 'f', 'Female', 'female' = 'Female';
run;
proc freq data=have;
tables gender;
format gender $gender.;
run;
This leaves open the possibility of not changing your data, but still grouping the values for reporting purposes. It's a decent approach for cleaning data, as the PROC FREQ output displays values that occur in the data but are not accounted for using the format.
If you did want to change the values, you could still apply the format:
data want;
set have;
sex = put(gender, $gender.);
run;
Hi @fengyuwuzu,
You could use character functions instead. Example:
data want;
set have;
length sex $1;
sex=lowcase(char(gender,1));
drop gender;
rename sex=gender;
run;
@fengyuwuzu: When using the formatting approach you may want to use the order=formatted option in PROC FREQ statements, so as to ensure a consistent ordering of the two categories in the output. (Otherwise, the sort order would depend on the data because, for example, 'F'<'M', but 'M'<'f'.)
Another possibility: don't recode. Create a format to translate:
proc format;
value $gender 'M', 'm', 'Male', 'male' = 'Male'
'F', 'f', 'Female', 'female' = 'Female';
run;
proc freq data=have;
tables gender;
format gender $gender.;
run;
This leaves open the possibility of not changing your data, but still grouping the values for reporting purposes. It's a decent approach for cleaning data, as the PROC FREQ output displays values that occur in the data but are not accounted for using the format.
If you did want to change the values, you could still apply the format:
data want;
set have;
sex = put(gender, $gender.);
run;
If I'm building the data set I often take @Astounding's idea one step earlie with an INFORMAT to read all of the values into my desired coding so later on all of the values a uniform.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.