BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kcvaldez98
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
SAS Super FREQ

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 = .;

 

 

Check out my Jedi SAS Tricks for SAS Users

View solution in original post

3 REPLIES 3
kcvaldez98
Obsidian | Level 7
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
68
69 DATA LAB.STUDY_COHORT_2;
70 FORMAT race_eth $30.;
71 SET LAB.STUDY_COHORT;
72
73 /* Gestational diabetes */
74 IF RFDiabGest = "True" THEN GestDiab= "Y";
75 ELSE GestDiab= "N";
76
77 /* Race/Ethnicity variable */
78 IF MatRace = " " OR MatEthnicity = " " THEN race_eth=" ";
79 ELSE IF SUBSTR(MatEthnicity,1,1) = "2" THEN race_eth="Hispanic (any race)";
80 ELSE IF SUBSTR(MatRace,2,1)="1" THEN race_eth = "Non-Hispanic White";
81 ELSE race_eth="Non-Hispanic Non-White";
82
83 /* Previous live births need to be converted to numeric variable */
84 prev_alive2 = INPUT(PREV_ALIVE, 2.);
85 IF PREV_ALIVE= . THEN PREV_ALIVE = .;
86
87 /* Convert mother's height to height in inches */
88 IF MotherHeight= . THEN Ht_inches = .;
89 ELSE Ht_inches=(INPUT(SUBSTR(MotherHeight,2,1),1.)*12)+(INPUT(SUBSTR(MotherHeight,4,2),2.));
90
91 /* Convert prior weight to a numeric variable */
92 IF priorweight = . THEN priorweight2 = .; /* Set to missing if unknown */
93 ELSE priorweight2= INPUT(priorweight,4.);
94
95 /* Calculate BMI */
96 IF priorweight2= . or Ht_inches= . THEN BMI = .; /* Set to missing if height or weight is missing */
97 ELSE BMI = ((priorweight2)/(Ht_inches**2))*703;
98
99 /* Categorize into Obese status */
100 IF BMI = . THEN ObesePrior = " "; /* Set to missing if BMI is missing */
101 ELSE IF BMI < 30 THEN ObesePrior = "N";
102 ELSE ObesePrior = "Y";
103
104 /* Variable for preterm birth */
105 IF EstGest > 36 THEN Preterm = "N";
106 ELSE Preterm = "Y";
107
108 /* Labeling new variables */
109 LABEL GestDiab = "Gestational Diabetes"
110 race_eth = "Race/Ethnicity"
111 prev_alive2 = "Previous Live Births"
112 Ht_inches = "Mother's Height (in inches)"
113 priorweight2 = "Prior Weight Before Pregnancy"
114 BMI = "Body Mass Index"
115 ObesePrior = "Obese Prior (Y/N)"
116 Preterm = "Gestation Age Before 37 Weeks";
117 RUN;
 
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
85:7 88:8 92:8 105:8
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
85:39
NOTE: Invalid numeric data, MotherHeight='05:07' , at line 88 column 8.
race_eth=Hispanic (any race) ID=2012000176 MotherHeight=05:07 PriorWeight=170 PREV_ALIVE=2 RFDiabGest= EstGest=39 EstGestOb=
EstGestClin=True Plurality=1 MatEthnicity=210 MatRace=01 M_AGE=31 GestDiab=N prev_alive2=2 Ht_inches=. priorweight2=170 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=1
NOTE: Invalid numeric data, MotherHeight='05:02' , at line 88 column 8.
race_eth=Hispanic (any race) ID=2012000324 MotherHeight=05:02 PriorWeight=220 PREV_ALIVE=1 RFDiabGest=True EstGest=38 EstGestOb=True
EstGestClin= Plurality=1 MatEthnicity=280 MatRace=01 M_AGE=32 GestDiab=Y prev_alive2=1 Ht_inches=. priorweight2=220 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=2
NOTE: Invalid numeric data, MotherHeight='05:01' , at line 88 column 8.
race_eth=Hispanic (any race) ID=2012000510 MotherHeight=05:01 PriorWeight=123 PREV_ALIVE=0 RFDiabGest= EstGest=39 EstGestOb=True
EstGestClin= Plurality=1 MatEthnicity=210 MatRace=01 M_AGE=20 GestDiab=N prev_alive2=0 Ht_inches=. priorweight2=123 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=3
NOTE: Invalid numeric data, MotherHeight='05:02' , at line 88 column 8.
race_eth=Hispanic (any race) ID=2012001165 MotherHeight=05:02 PriorWeight=145 PREV_ALIVE=0 RFDiabGest= EstGest=38 EstGestOb=
EstGestClin=True Plurality=1 MatEthnicity=210 MatRace=01 M_AGE=22 GestDiab=N prev_alive2=0 Ht_inches=. priorweight2=145 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=4
NOTE: Invalid numeric data, MotherHeight='05:04' , at line 88 column 8.
race_eth=Non-Hispanic White ID=2012001472 MotherHeight=05:04 PriorWeight=194 PREV_ALIVE=3 RFDiabGest= EstGest=39 EstGestOb=
EstGestClin=True Plurality=1 MatEthnicity=100 MatRace=01 M_AGE=35 GestDiab=N prev_alive2=3 Ht_inches=. priorweight2=194 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=5
NOTE: Invalid numeric data, MotherHeight='05:07' , at line 88 column 8.
race_eth=Non-Hispanic White ID=2012001710 MotherHeight=05:07 PriorWeight=123 PREV_ALIVE=0 RFDiabGest= EstGest=41 EstGestOb=True
EstGestClin= Plurality=1 MatEthnicity=100 MatRace=01 M_AGE=30 GestDiab=N prev_alive2=0 Ht_inches=. priorweight2=123 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=6
NOTE: Invalid numeric data, MotherHeight='05:04' , at line 88 column 8.
race_eth=Non-Hispanic White ID=2012001836 MotherHeight=05:04 PriorWeight=132 PREV_ALIVE=0 RFDiabGest= EstGest=40 EstGestOb=True
EstGestClin= Plurality=1 MatEthnicity=100 MatRace=01 M_AGE=30 GestDiab=N prev_alive2=0 Ht_inches=. priorweight2=132 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=7
NOTE: Invalid numeric data, MotherHeight='05:03' , at line 88 column 8.
race_eth=Non-Hispanic White ID=2012001863 MotherHeight=05:03 PriorWeight=149 PREV_ALIVE=1 RFDiabGest= EstGest=40 EstGestOb=
EstGestClin=True Plurality=1 MatEthnicity=100 MatRace=01 M_AGE=30 GestDiab=N prev_alive2=1 Ht_inches=. priorweight2=149 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=8
NOTE: Invalid numeric data, MotherHeight='04:08' , at line 88 column 8.
race_eth=Hispanic (any race) ID=2012002184 MotherHeight=04:08 PriorWeight=128 PREV_ALIVE=0 RFDiabGest= EstGest=41 EstGestOb=True
EstGestClin= Plurality=1 MatEthnicity=210 MatRace=01 M_AGE=23 GestDiab=N prev_alive2=0 Ht_inches=. priorweight2=128 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=9
NOTE: Invalid numeric data, MotherHeight='05:00' , at line 88 column 8.
race_eth=Hispanic (any race) ID=2012002326 MotherHeight=05:00 PriorWeight=160 PREV_ALIVE=0 RFDiabGest= EstGest=37 EstGestOb=True
EstGestClin= Plurality=1 MatEthnicity=281 MatRace=01 M_AGE=20 GestDiab=N prev_alive2=0 Ht_inches=. priorweight2=160 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=10
NOTE: Invalid numeric data, MotherHeight='04:11' , at line 88 column 8.
race_eth=Hispanic (any race) ID=2012002393 MotherHeight=04:11 PriorWeight=122 PREV_ALIVE=8 RFDiabGest= EstGest=38 EstGestOb=True
EstGestClin= Plurality=1 MatEthnicity=210 MatRace=01 M_AGE=38 GestDiab=N prev_alive2=8 Ht_inches=. priorweight2=122 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=11
NOTE: Invalid numeric data, MotherHeight='04:11' , at line 88 column 8.
race_eth=Hispanic (any race) ID=2012002548 MotherHeight=04:11 PriorWeight=143 PREV_ALIVE=1 RFDiabGest= EstGest=37 EstGestOb=True
EstGestClin= Plurality=1 MatEthnicity=210 MatRace=01 M_AGE=28 GestDiab=N prev_alive2=1 Ht_inches=. priorweight2=143 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=12
NOTE: Invalid numeric data, MotherHeight='05:02' , at line 88 column 8.
race_eth=Hispanic (any race) ID=2012003049 MotherHeight=05:02 PriorWeight=125 PREV_ALIVE=1 RFDiabGest= EstGest=41 EstGestOb=
EstGestClin=True Plurality=1 MatEthnicity=210 MatRace=01 M_AGE=20 GestDiab=N prev_alive2=1 Ht_inches=. priorweight2=125 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=13
NOTE: Invalid numeric data, MotherHeight='04:11' , at line 88 column 8.
race_eth=Non-Hispanic White ID=2012003074 MotherHeight=04:11 PriorWeight=195 PREV_ALIVE=1 RFDiabGest= EstGest=35 EstGestOb=True
EstGestClin= Plurality=1 MatEthnicity=100 MatRace=01 M_AGE=28 GestDiab=N prev_alive2=1 Ht_inches=. priorweight2=195 BMI=.
ObesePrior= Preterm=Y _ERROR_=1 _N_=14
NOTE: Invalid numeric data, MotherHeight='05:04' , at line 88 column 8.
race_eth=Hispanic (any race) ID=2012003126 MotherHeight=05:04 PriorWeight=160 PREV_ALIVE=1 RFDiabGest= EstGest=40 EstGestOb=
EstGestClin=True Plurality=1 MatEthnicity=210 MatRace=01 M_AGE=23 GestDiab=N prev_alive2=1 Ht_inches=. priorweight2=160 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=15
NOTE: Invalid numeric data, MotherHeight='05:06' , at line 88 column 8.
race_eth=Non-Hispanic White ID=2012003186 MotherHeight=05:06 PriorWeight=130 PREV_ALIVE=0 RFDiabGest= EstGest=39 EstGestOb=
EstGestClin=True Plurality=1 MatEthnicity=100 MatRace=01 M_AGE=30 GestDiab=N prev_alive2=0 Ht_inches=. priorweight2=130 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=16
NOTE: Invalid numeric data, MotherHeight='05:07' , at line 88 column 8.
race_eth=Non-Hispanic White ID=2012003306 MotherHeight=05:07 PriorWeight=137 PREV_ALIVE=0 RFDiabGest= EstGest=37 EstGestOb=True
EstGestClin= Plurality=1 MatEthnicity=100 MatRace=01 M_AGE=21 GestDiab=N prev_alive2=0 Ht_inches=. priorweight2=137 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=17
NOTE: Invalid numeric data, MotherHeight='05:01' , at line 88 column 8.
race_eth=Hispanic (any race) ID=2012003437 MotherHeight=05:01 PriorWeight=130 PREV_ALIVE=0 RFDiabGest= EstGest=39 EstGestOb=
EstGestClin=True Plurality=1 MatEthnicity=281 MatRace=01 M_AGE=16 GestDiab=N prev_alive2=0 Ht_inches=. priorweight2=130 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=18
NOTE: Invalid numeric data, MotherHeight='05:02' , at line 88 column 8.
NOTE: Invalid numeric data, PriorWeight='?' , at line 92 column 8.
race_eth=Non-Hispanic Non-White ID=2012003508 MotherHeight=05:02 PriorWeight=? PREV_ALIVE=3 RFDiabGest= EstGest=39 EstGestOb=
EstGestClin=True Plurality=1 MatEthnicity=100 MatRace=10 M_AGE=24 GestDiab=N prev_alive2=3 Ht_inches=. priorweight2=. BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=19
NOTE: Invalid numeric data, MotherHeight='05:01' , at line 88 column 8.
WARNING: Limit set by ERRORS= option reached. Further errors of this type will not be printed.
race_eth=Non-Hispanic White ID=2012003665 MotherHeight=05:01 PriorWeight=180 PREV_ALIVE=1 RFDiabGest= EstGest=40 EstGestOb=
EstGestClin=True Plurality=1 MatEthnicity=100 MatRace=01 M_AGE=23 GestDiab=N prev_alive2=1 Ht_inches=. priorweight2=180 BMI=.
ObesePrior= Preterm=N _ERROR_=1 _N_=20
NOTE: There were 292 observations read from the data set LAB.STUDY_COHORT.
NOTE: The data set LAB.STUDY_COHORT_2 has 292 observations and 20 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
user cpu time 0.01 seconds
system cpu time 0.01 seconds
memory 1009.37k
OS Memory 23464.00k
Timestamp 02/28/2024 03:40:49 PM
Step Count 34 Switch Count 2
Page Faults 0
Page Reclaims 173
Page Swaps 0
Voluntary Context Switches 44
Involuntary Context Switches 0
Block Input Operations 32
Block Output Operations 280
 
 
118
119 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
129
SASJedi
SAS Super FREQ

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 = .;

 

 

Check out my Jedi SAS Tricks for SAS Users

SAS Innovate 2025: Register Now

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!

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
  • 3 replies
  • 2009 views
  • 1 like
  • 2 in conversation