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. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 1514 views
  • 3 likes
  • 4 in conversation