Hi all;
I'm trying to create a new set of variables using IF/THEN/ELSE statements, but I'm receiving an error saying that there's a syntax error and one saying that the statement is not valid. See below for example code (using SAS Studio 9.4 M6).
So far, I've verified the correct variable names and spellings, the specified ranges do not overlap, and each statement appears to be independent of one another. My sense is that there's a minor piece missing, rather than several lines of code, but any assistance is welcomed.
If that screenshot is the exact code you ran, there are several problems:
Try this instead:
data WORK.MISSISSIPPI_VS;
input SSN: $11. VisitDate Height Weight SBP DBP;
datalines;
000-00-0000 18504 65 153 111 72
000-00-0000 18861 65 158 109 75
000-00-0000 19236 65 151 115 74
000-00-0000 19606 65 154 107 79
000-00-0000 19978 65 153 110 74
;
run;
data want;
set mississippi_vs;
format SBPHypCat $32.;
if SBP = . then SBPHypCat = ' ';
else if SBP lt 90 then SBPHypCat = 'Below Normal';
else if 90 le SBP lt 120 then SBPHypCat = 'Normal';
else if 120 le SBP lt 140 then SBPHypCat = 'High Normal';
else if 140 le SBP lt 160 then SBPHypCat = 'Stage 1 Hypertension';
else if 160 le SBP lt 180 then SBPHypCat = 'Stage 2 Hypertension';
else if 180 le SBP then SBPHypCat = 'Stage 3 Hypertension';
run;
It appears that your code does not have space characters between elements.
90THENSBPHypCat
should be
90 THEN SBPHypCat
If you copied that code from somewhere else then you may have characters that in some file formats appear to be spaces but are not used as such in the editor/ code parse by SAS.
I also suspect that you have copied some code from a Proc Format example because the - < is not normally used for comparison in data step code. Maybe.
When including error messages include 1) The entire Procedure or DATA step code and 2) copy the text from the log with the messages and errors. Paste into a code box opened on the forum with the </> icon to preserve formatting of the diagnostic characters such as the underscores.
It is much easier to suggest changes by copy/paste and edit code than from pictures.
Thanks for the reply; unfortunately I'll still need a hand. I'm not sure why the log says there are no spaces, but there are (see photo). I am also attaching the full log, in case there is anything else there that can help.
Looks like your code came into contact with a word processor and now has "funny" spaces. Your best bet is to delete it and retype it by hand.
You are also trying to use PROC FORMAT syntax in a data step. Change the conditions to (example)
if 90 le sbp lt 120 then sbphypcat = "Normal";
If that screenshot is the exact code you ran, there are several problems:
Try this instead:
data WORK.MISSISSIPPI_VS;
input SSN: $11. VisitDate Height Weight SBP DBP;
datalines;
000-00-0000 18504 65 153 111 72
000-00-0000 18861 65 158 109 75
000-00-0000 19236 65 151 115 74
000-00-0000 19606 65 154 107 79
000-00-0000 19978 65 153 110 74
;
run;
data want;
set mississippi_vs;
format SBPHypCat $32.;
if SBP = . then SBPHypCat = ' ';
else if SBP lt 90 then SBPHypCat = 'Below Normal';
else if 90 le SBP lt 120 then SBPHypCat = 'Normal';
else if 120 le SBP lt 140 then SBPHypCat = 'High Normal';
else if 140 le SBP lt 160 then SBPHypCat = 'Stage 1 Hypertension';
else if 160 le SBP lt 180 then SBPHypCat = 'Stage 2 Hypertension';
else if 180 le SBP then SBPHypCat = 'Stage 3 Hypertension';
run;
Thank you for the reply. Fair point; I missed the name that's in the length statement. That's been updated. Also, the spaces are there; somehow the log is reporting they're not there (see above picture). I tried adding additional space, just in case, but no change. As for the operators, my guess is that's where the issue is coming into play.
@dwrightii Here's another way to do it, using a PROC FORMAT:
data WORK.MISSISSIPPI_VS;
input SSN: $11. VisitDate Height Weight SBP DBP;
datalines;
000-00-0000 18504 65 153 25 72
000-00-0000 18861 65 158 95 75
000-00-0000 19236 65 151 125 74
000-00-0000 19606 65 154 140 79
000-00-0000 19978 65 153 161 74
000-00-0000 19978 65 153 214 74
;
run;
proc format;
value SBP_format . = ' '
low - < 90 = 'Below Normal'
90 - < 120 = 'Normal'
120 - < 140 = 'High Normal'
140 - < 160 = 'Stage 1 Hypertension'
160 - < 180 = 'Stage 2 Hypertension'
180 - high = 'Stage 3 Hypertension'
;
run;
data want2;
set mississippi_vs;
SBPHypCat = put(SBP,SBP_format.);
run;
(Note: I made up some dummy SBP values, to test each of the cases.)
That idea will definitely work well, although this particular data set is a bit too large to go the datalines route . Still, that is good to know for my other projects - thank you!
Thanks to everyone for your help. Here is what I ended up going with:
IF SBP = . THEN SBPHypCat = ' ';
ELSE IF SBP < 90 THEN SBPHypCat = 'Below Normal';
ELSE IF SBP < 120 THEN SBPHypCat = 'Normal';
ELSE IF SBP < 140 THEN SBPHypCat = 'High Normal';
ELSE IF SBP < 160 THEN SBPHypCat = 'Stage 1 Hypertension';
ELSE IF SBP < 180 THEN SBPHypCat = 'Stage 2 Hypertension';
ELSE SBPHypCat = 'Stage 3 Hypertension';
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.