Hello,
I am manipulating my data using the following code:
/*Convert motherheight to height in inches*/
DATA LabAnal.study_cohort_2;
SET LabAnal.study_cohort;
IF MotherHeight = '' THEN Ht_Inches = .;
ELSE IF MotherHeight ='?' THEN Ht_Inches ='';
ELSE Ht_Inches=(INPUT(SUBSTR(MotherHeight, 1, 2), 2.)*12) + (INPUT(SUBSTR(MotherHeight, 4, 2),2.));
RUN;
^^this works great
/*Convert priorweight to a numeric variable*/
DATA LabAnal.study_cohort_2;
SET LabAnal.study_cohort;
Priorweight2 = INPUT(PriorWeight, 3.);
RUN;
^^this also works great
/*Calculate BMI*/
DATA LabAnal.study_cohort_2;
SET LabAnal.study_cohort;
IF Priorweight2= "." or Ht_inches= "." THEN BMI= ".";
ELSE BMI= (Priorweight2*703)/(Ht_Inches*2);
RUN;
^^but when I run this, it sets all the values from priorweight2, ht_inches and BMI to missing. I've tried with and without quotes around the periods in the IF statement for BMI code. It doesn't change anything.
As you might have guessed, there are problems with your final step. The main problem is here:
/*Calculate BMI*/
DATA LabAnal.study_cohort_2;
SET LabAnal.study_cohort;
You are using the wrong data set. This step replaces LabAnal.study_cohort_2, by reading in the data rom LabAnal.study_cohort (which does not contain Priorweight2).
There are other, minor issues. For example, to refer to a missing value for a numeric variable, do not use quotes. Change the next line to:
IF Priorweight2= . or Ht_inches= . THEN BMI= .;
As you might have guessed, there are problems with your final step. The main problem is here:
/*Calculate BMI*/
DATA LabAnal.study_cohort_2;
SET LabAnal.study_cohort;
You are using the wrong data set. This step replaces LabAnal.study_cohort_2, by reading in the data rom LabAnal.study_cohort (which does not contain Priorweight2).
There are other, minor issues. For example, to refer to a missing value for a numeric variable, do not use quotes. Change the next line to:
IF Priorweight2= . or Ht_inches= . THEN BMI= .;
I completely overlooked the DATA SET. That was the fix. Thank You!!
The story changes depending on the attributes of the variables, so please provide reproducible data.
Without seeing the data, I can't say exactly what you're seeing.
Perhaps,
IF Priorweight2= "." OR Ht_inches= "." THEN BMI= "." ;
this statement is the problem.
Not . which means missing but "." which is string is stored in Priorweight2, Ht_inches, and BMI.
IF Priorweight2= . . or Ht_inches= . THEN BMI= . ;
would improve this point.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.