I am trying to convert the character variable "GenderCd" to a numeric variable. In order to apply a numeric format 'GenderCd.' I have tried using the INPUT statement to convert the variable into a numeric one, but it doesn't seem to be working properly. I'm not sure what I'm doing incorrectly.
This is the following code I've input into SAS:
DATA WORK.Demog_IA;
KEEP SSN GenderCd EthCd RaceCd EthRaceCd BirthDt;
SET IowaResidents;
LENGTH SSN $11
GenderCd
EthCd $1
RaceCd $1
EthRaceCd $3;
Gender = INPUT(Sex,1.);
Gender= PROPCASE(Sex);
IF Gender = 'Male' THEN GenderCd = 1;
ELSE IF Gender = 'Female' Then GenderCd = 2;
IF MISSING(Ethnicity) = 1 THEN EthCd = ' ';
ELSE IF Ethnicity = 'HISPANIC' THEN EthCd = 'H';
ELSE IF Ethnicity = 'NON-HISPANIC' THEN EthCd = 'N';
Else EthCd = 'U';
IF MISSING(Race) = 1 THEN RaceCd = ' ';
ELSE IF Race = 'WHITE' THEN RaceCd = 'W';
ELSE IF Race = 'BLACK' THEN RaceCd = 'B';
ELSE IF Race = 'ASIAN' THEN RaceCd = 'A';
ELSE IF Race = 'OTHER' THEN RaceCd = 'O';
ELSE RaceCd = 'U';
IF MISSING(EthCd) = ' ' AND RaceCd = ' ' THEN EthRaceCd = ' ';
ELSE IF EthCd = 'H' THEN EthRaceCd = 'HIS';
ELSE IF RaceCd = 'A' THEN EthRaceCd = 'NHA';
ELSE IF RaceCd = 'B' THEN EthRaceCd = 'NHB';
ELSE IF RaceCd = 'W' THEN EthRaceCd = 'NHW';
ELSE IF RaceCd = 'O' THEN EthRaceCd = 'NHO';
ELSE EthRaceCd = '.';
RUN;
PROC PRINT DATA = WORK.Demog_IA;
FORMAT GenderCd GenderCd.
EthCd $EthCd.
RaceCd $RaceCd.
EthRaceCd $EthRaceCd.
BirthDt MMDDYY10.;
RUN;
When I attempt to apply the format to the variables I receive the following error message:
Gendercd, as you sais, is char type.
According to log "You are trying to use the numeric format GENDERCD with the character variable GenderCd in data set"
But your numeric variable is GENDER [in code: Gender = INPUT(Sex,1.);]
Change the line in proc print to:
FORMAT Gender GenderCd.
instead: FORMAT GenderCd GenderCd.
and add VAR statement to define the variables to print and their order.
Are those real SSNs in your screenshot? If so you should consider removing them for privacy reasons.
Hello, they are not. The data set is fake and is for a class.
LENGTH SSN $11
GenderCd /* by not supplying anything here, gendercd will be defined as $1, along with ethcd */
EthCd $1
RaceCd $1
EthRaceCd $3;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.