I need to create a gender variable from a Danish social security number. I am using SAS University edition for mac.
I want my data to look like this:
social security number gender
xxxxxx-xxx7 1
xxxxxx-xxx4 0
xxxxxx-xxx3 1
xxxxxx-xxx2 0
xxxxxx-xxx1 1
xxxxxx-xxx8 0
when the last digit is an even number it is a female and uneven number is a male.
I have used proc format to create a new variable, but I that's all I have been able to do.
proc format
value gender
0="Female"
1="Male";
run;
Thanks!
like this?
data have;
input cpr $20.;
datalines;
280279-1667
280279-1668
280279-1667
;
data want;
set have;
if substr(cpr,length(cpr),1)in ("0","2","4","6","8") then gender=0;
else gender=1;
run;
like this?
data have;
input cpr $20.;
datalines;
280279-1667
280279-1668
280279-1667
;
data want;
set have;
if substr(cpr,length(cpr),1)in ("0","2","4","6","8") then gender=0;
else gender=1;
run;
You can apply your format from your PROC FORMAT afterwards, but beware that you are missing a semicolon after PROC FORMAT statement. It should be
proc format;
value gender
0="Female"
1="Male";
run;
proc format does NOT create a new variable. It only controls how a variable is displayed.
In your case you need to create a new variable to which your new format can be displayed. Assuming that the Danish social security number is a character variable, you can just retrieve the 11th character with the CHAR function, convert it to a numeric value, and then find the remainder after division by 2:
proc format;
value gender
0="Female"
1="Male";
run;
data t;
input ssn $11.;
g=input(char(ssn,11),1.);
g=mod(g,2);
format g gender.;
put (_all_) (=);
datalines;
xxxxxx-xxx7
xxxxxx-xxx4
xxxxxx-xxx3
xxxxxx-xxx2
xxxxxx-xxx1
xxxxxx-xxx8
run;
I need to create a gender variable from a Danish social security number.
I want my data to look like this:
social security number gender
xxxxxx-xxx7 1
xxxxxx-xxx4 0
xxxxxx-xxx3 1
xxxxxx-xxx2 0
xxxxxx-xxx1 1
xxxxxx-xxx8 0
when the last digit is an even number it is a female and uneven number is a male.
I have used proc format to create a new variable, but I that's all I have been able to do.
proc format
value gender
0="Female"
1="Male";
run;
Thanks!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Get started using SAS Studio to write, run and debug your SAS programs.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.