Hi SAS users!
I am doing some SAS homework and ran into this warning which won't let me convert character into numeric and therefore I am unable to run half this code. My goal is for prev_alive2 to be numeric and Ht_Inches to be numeric so I can calculate the BMI so I can have the BMI to look at mothers who are obese prior to pregnancy. Let me show you my code: and please let me know where I went wrong.
DATA LAB.STUDY_COHORT_2;
FORMAT race_eth $30.;
SET LAB.STUDY_COHORT;
/* Gestational diabetes */
IF RFDiabGest = "True" THEN GestDiab= "Y";
ELSE GestDiab= "N";
/* Race/Ethnicity variable */
IF MatRace = " " OR MatEthnicity = " " THEN race_eth=" ";
ELSE IF SUBSTR(MatEthnicity,1,1) = "2" THEN race_eth="Hispanic (any race)";
ELSE IF SUBSTR(MatRace,2,1)="1" THEN race_eth = "Non-Hispanic White";
ELSE race_eth="Non-Hispanic Non-White";
/* Previous live births need to be converted to numeric variable */
prev_alive2 = INPUT(PREV_ALIVE, 2.);
IF PREV_ALIVE= . THEN PREV_ALIVE = .;
/* Convert mother's height to height in inches */
IF MotherHeight= . THEN Ht_inches = .;
ELSE Ht_inches=(INPUT(SUBSTR(MotherHeight,2,1),1.)*12)+(INPUT(SUBSTR(MotherHeight,4,2),2.));
/* Convert prior weight to a numeric variable */
IF priorweight = . THEN priorweight2 = .; /* Set to missing if unknown */
ELSE priorweight2= INPUT(priorweight,4.);
/* Calculate BMI */
IF priorweight2= . or Ht_inches= . THEN BMI = .; /* Set to missing if height or weight is missing */
ELSE BMI = ((priorweight2)/(Ht_inches**2))*703;
/* Categorize into Obese status */
IF BMI = . THEN ObesePrior = " "; /* Set to missing if BMI is missing */
ELSE IF BMI < 30 THEN ObesePrior = "N";
ELSE ObesePrior = "Y";
/* Variable for preterm birth */
IF EstGest > 36 THEN Preterm = "N";
ELSE Preterm = "Y";
/* Labeling new variables */
LABEL GestDiab = "Gestational Diabetes"
race_eth = "Race/Ethnicity"
prev_alive2 = "Previous Live Births"
Ht_inches = "Mother's Height (in inches)"
priorweight2 = "Prior Weight Before Pregnancy"
BMI = "Body Mass Index"
ObesePrior = "Obese Prior (Y/N)"
Preterm = "Gestation Age Before 37 Weeks";
RUN;
The dataset looks like this:
Obs | ID | MotherHeight | PriorWeight | PREV_ALIVE | RFDiabGest | EstGest | EstGestOb | EstGestClin | Plurality | MatEthnicity | MatRace | M_AGE |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 2012000176 | 05:07 | 170 | 2 | 39 | True | 1 | 210 | 01 | 31 | ||
2 | 2012000324 | 05:02 | 220 | 1 | True | 38 | True | 1 | 280 | 01 | 32 | |
3 | 2012000510 | 05:01 | 123 | 0 | 39 | True | 1 | 210 | 01 | 20 | ||
4 | 2012001165 | 05:02 | 145 | 0 | 38 | True | 1 | 210 | 01 | 22 | ||
5 | 2012001472 | 05:04 | 194 | 3 | 39 | True | 1 | 100 | 01 | 35 | ||
6 | 2012001710 | 05:07 | 123 | 0 | 41 | True | 1 | 100 | 01 | 30 | ||
7 | 2012001836 | 05:04 | 132 | 0 | 40 | True | 1 | 100 | 01 | 30 | ||
8 | 2012001863 | 05:03 | 149 | 1 | 40 | True | 1 | 100 | 01 | 30 | ||
9 | 2012002184 | 04:08 | 128 | 0 | 41 | True | 1 | 210 | 01 | 23 | ||
10 | 2012002326 | 05:00 | 160 | 0 | 37 | True | 1 | 281 | 01 | 20 | ||
11 | 2012002393 | 04:11 | 122 | 8 | 38 | True | 1 | 210 | 01 | 38 | ||
12 | 2012002548 | 04:11 | 143 | 1 | 37 | True | 1 | 210 | 01 | 28 | ||
13 | 2012003049 | 05:02 | 125 | 1 | 41 | True | 1 | 210 | 01 | 20 | ||
14 | 2012003074 | 04:11 | 195 | 1 | 35 | True | 1 | 100 | 01 | 28 | ||
15 | 2012003126 | 05:04 | 160 | 1 | 40 | True | 1 | 210 | 01 | 23 | ||
16 | 2012003186 | 05:06 | 130 | 0 | 39 | True | 1 | 100 | 01 | 30 | ||
17 | 2012003306 | 05:07 | 137 | 0 | 37 | True | 1 | 100 | 01 | 21 | ||
18 | 2012003437 | 05:01 | 130 | 0 | 39 | True | 1 | 281 | 01 | 16 | ||
19 | 2012003508 | 05:02 | ? | 3 | 39 | True | 1 | 100 | 10 | 24 | ||
20 | 2012003665 | 05:01 | 180 | 1 | 40 | True | 1 | 100 | 01 | 23 | ||
21 | 2012003987 | 05:06 | 178 | 0 | 39 | True | 1 | 100 | 01 | 31 | ||
22 | 2012004002 | 05:03 | 154 | 0 | True | 36 | True | 1 | 100 | 01 | 27 | |
23 | 2012004481 | 04:07 | 131 | 0 | 40 | True | 1 | 210 | 01 | 36 | ||
24 | 2012004529 | 05:09 | 175 | 1 | 39 | True | 1 | 100 | 01 | 31 | ||
25 | 2012004620 | 05:08 | 219 | 2 | 39 | True | 1 | 210 | 15 | 22 | ||
26 | 2012004670 | 05:06 | 190 | 1 | 39 | True | 1 | 100 | 01 | 34 | ||
27 | 2012004797 | 05:04 | 145 | 1 | 40 | True | 1 | 210 | 01 | 22 | ||
28 | 2012005112 | 05:04 | 174 | 2 | 39 | True | 1 | 210 | 01 | 30 | ||
29 | 2012005419 | 05:05 | 141 | 0 | 40 | True | 1 | 100 | 01 | 29 | ||
30 | 2012005472 | 05:03 | 120 | 0 | 38 | True | 1 | 100 | 01 | 27 | ||
31 | 2012005558 | 05:08 | 261 | 1 | 39 | True | 1 | 100 | 01 | 29 | ||
32 | 2012005704 | 05:04 | ? | 2 | 40 | True | 1 | 210 | 01 |
27 |
The error is caused by attempting to compare a character value (MotherHeight) to a numeric MISSING value (.) in this line of code:
/* Convert mother's height to height in inches */
IF MotherHeight= . THEN Ht_inches = .;
Using the MISSING function will resolve the problem:
/* Convert mother's height to height in inches */
IF MISSING(MotherHeight) THEN Ht_inches = .;
The error is caused by attempting to compare a character value (MotherHeight) to a numeric MISSING value (.) in this line of code:
/* Convert mother's height to height in inches */
IF MotherHeight= . THEN Ht_inches = .;
Using the MISSING function will resolve the problem:
/* Convert mother's height to height in inches */
IF MISSING(MotherHeight) THEN Ht_inches = .;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.