BookmarkSubscribeRSS Feed
GoldingAngela
Fluorite | Level 6

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:

ERROR: You are trying to use the numeric format GENDERCD with the character variable GenderCd in data set WORK.DEMOG_IA.

 

 

4 REPLIES 4
Shmuel
Garnet | Level 18

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.

SASKiwi
PROC Star

Are those real SSNs in your screenshot? If so you should consider removing them for privacy reasons.

GoldingAngela
Fluorite | Level 6

Hello, they are not. The data set is fake and is for a class. 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 1112 views
  • 3 likes
  • 4 in conversation