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!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.