1
votes

I'm working on some SAS code and ran into an issue. I'm trying to calculate BMI for 6000 plus patients (So datalines isn't an option) using weight and height. Height is currently entered as 508 for 5 feet, 08 inches and so on for every obs. So I figured I would try to combine a substr and input statement to pull the first digit from the 3 digit code, and then do the same for inches. I'll be able to do the rest when I have these two new variables, but I cant seem to get this particular data step to work.

this is what I have:

data work.bmi_prep;
set work.fixedmissing;
HEIGHTFT=input(substr(HEIGHT3,1,1),3.);
run;

if someone can tell me where I'm going wrong that would be great.

1
What is the error or undesired output that you are getting?NEOmen
I suppose it could be an issue with leading spaces..use compress function like this ..HEIGHTFT=input(substr(compress(HEIGHT3),1,1),3.);NEOmen
your variable HEIGHT3 character or numeric? If it is numeric then use put function like HEIGHTFT=input(substr(put(HEIGHT3,3.),1,1),3.); strip function also can be used.Kay
there are three main reasons for getting errors with that code: 1) numeric or character in the source and in the output variable, use put or input according to this difference. 2) substr accept only char variables. 3) character variables are at the left of the total length (trailing blanks), numeric are at the right of the cell length (leading blanks), please use left() function (or similar string functions) if you are converting from num to charstat

1 Answers

0
votes

as others said in the comments the error you're encountering is probably due to leading spaces in the input string. A good rule when dealing with character variables that contain numbers is to apply a left() or strip() or compress() function before using or extracting digits from that variable.

In your example going with string-handling functions (as input & substr) is not the easiest way to process the HEIGHT3 variable. You can use the automatic character to numeric conversion that SAS applies when finds a character variable in a mathematical formula.

In the following code the heightft formula will trigger this conversion, automatically handling the leading spaces.

data test;
    height3= '   508';
    heightft= int(height3/100);
    heightin= height3-(heightft*100);
run;

The presence of non-numeric characters in the height3 variable will lead to missing values exactly as if you were using the input() function.