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

Hi SAS Forum,

I need to categorize the subjects of my large dataset into the cells indicated as xxxx in below table.

This table's column variable is "Bal-band" while raw variable is 'beac_band".

                           

Bal_band
Beac_band3000+1001-3000<1000
1-200 xxxx xxxx
201-610 xxxx
611-700
700+
0 or none xxxx

First, at the highest level subjects have to be filtered like this.

if b_status = 'flcv' and sta_in_12 = '1-30' and use_band ='<100' then do;

To create a flag_variable so that I can identify the subjects to be allocated into

Cells of our intrest in the above table, I have tried the below code.

data want;

set have;

if b_status = 'flcv' and sta_in_12 = '1-30' and use_band ='<100' then do;

     if beac_band in ('1-200', ’201-600') and bal_band in ('3000+')

or

if beac_band in ('1-200') and bal_band in('1001-3000')

or

if beac_band in ('0 or none') and bal_band in('3000+')

then Flag = 'Medium';

     end;

   

if …………then do;

……..

…….

End;

un

if …………then do;

……..

…….

End;

RUN;

Question:

An error messege comes like this.

beac_band in ('580-620')

                                                                                                       ___________

                                                                                                       22

18       ! and bal_band in('3000+') then Fog = 'Medium';

ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, (, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT,

              IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, [, ^=, {, |, ||, ~=. 

Could you please let me know whether I can or cannot use the above code with several “or”  “or”   “or”.

Thank you for your help.

Mirisage

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
Diamond | Level 26

Hi:

  I believe that you want an ELSE where you show the use of OR. In a SAS IF statement, OR is a logical operator and is meant to be used like this:

IF (name='Alfred' and height = 69) OR name='Alice' then somevar=1;

ELSE IF name='William' OR (name='Jane' and height = 59.8) then somevar=2;

ELSE somevar=3;

  For more information about building IF statements, this is a good documentation topic:

SAS(R) 9.3 Statements: Reference

  When you need to execute more than 1 statement as a result of a condition being true, then you need to use a DO/END block with your IF statement:

IF (name='Alfred' and height = 69) OR name='Alice' then DO;

     somevar=1;

     newvar = x * y;

END;

ELSE IF name='William' OR (name='Jane' and height = 59.8) then DO;

   somevar=2;

   newvar = y*z;

END;

ELSE DO;

   somevar=3;

   newvar = x*z;

END;

cynthia

View solution in original post

2 REPLIES 2
SASKiwi
PROC Star

Try removing redundant IFs from this statement and add brackets like so:

     if (beac_band in ('1-200', ’201-600') and bal_band in ('3000+'))

or

(beac_band in ('1-200') and bal_band in('1001-3000'))

or

(beac_band in ('0 or none') and bal_band in('3000+'))

then Flag = 'Medium';

Cynthia_sas
Diamond | Level 26

Hi:

  I believe that you want an ELSE where you show the use of OR. In a SAS IF statement, OR is a logical operator and is meant to be used like this:

IF (name='Alfred' and height = 69) OR name='Alice' then somevar=1;

ELSE IF name='William' OR (name='Jane' and height = 59.8) then somevar=2;

ELSE somevar=3;

  For more information about building IF statements, this is a good documentation topic:

SAS(R) 9.3 Statements: Reference

  When you need to execute more than 1 statement as a result of a condition being true, then you need to use a DO/END block with your IF statement:

IF (name='Alfred' and height = 69) OR name='Alice' then DO;

     somevar=1;

     newvar = x * y;

END;

ELSE IF name='William' OR (name='Jane' and height = 59.8) then DO;

   somevar=2;

   newvar = y*z;

END;

ELSE DO;

   somevar=3;

   newvar = x*z;

END;

cynthia

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1060 views
  • 3 likes
  • 3 in conversation