BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dwrightii
Fluorite | Level 6

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.

 

data WORK.MISSISSIPPI_LT;
infile datalines dsd truncover;
input SSN:$11. VisitDate:32. Height:32. Weight:32. SBP:32. DBP:32.;
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 
;;;;
 
Error in Code.png
Dennis Wright, II
1 ACCEPTED SOLUTION

Accepted Solutions
mklangley
Lapis Lazuli | Level 10

If that screenshot is the exact code you ran, there are several problems:

  • You're missing some spaces between keywords and variable names (e.g. "IFSBP" and "90THENSBPHypCat"); that will never work.
  • The range syntax is not quite correct.

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;

 

 

View solution in original post

8 REPLIES 8
ballardw
Super User

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.

 

 

 

dwrightii
Fluorite | Level 6

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. Post Followup.png

Dennis Wright, II
Kurt_Bremser
Super User

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";
mklangley
Lapis Lazuli | Level 10

If that screenshot is the exact code you ran, there are several problems:

  • You're missing some spaces between keywords and variable names (e.g. "IFSBP" and "90THENSBPHypCat"); that will never work.
  • The range syntax is not quite correct.

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;

 

 

dwrightii
Fluorite | Level 6

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. 

Dennis Wright, II
mklangley
Lapis Lazuli | Level 10

@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.)

dwrightii
Fluorite | Level 6

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!

Dennis Wright, II
dwrightii
Fluorite | Level 6

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';

Dennis Wright, II

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 1306 views
  • 0 likes
  • 4 in conversation