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

I want to use multiple "and" criteria with an if statement. The and (bold, italic, and underlined in the code below) is not blue in my SAS program...can I use more than two and's within an if function?

 

data temp.compQTR_WinRaw2a; set temp.compQTR_WinRaw2;
if (datacqtr = .) and (fyr = "12") then datacqtr = datafqtr;
if (datacqtr = .) and (fyr = "3") and (fqtr = "4") then datacqtr = cats(fyearq, "Q1");
run;

 

I greatly appreciate any help.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@jaimedba 

Multiple AND are totally o.k.

Also use ELSE if there is a set of multiple related IF conditions. 

data temp.compQTR_WinRaw2a; 
  set temp.compQTR_WinRaw2;
  if missing(datacqtr) and fyr = "12" then datacqtr = datafqtr;
  else if missing(datacqtr) and fyr in ('3','4') then datacqtr = cats(fyearq, "Q1");
run;

And looking at your code guessing what you're doing: You will at one point in the rather near future also have to learn how you can work with SAS date values and formats. Once you master this working with dates in SAS will become easier.

 

Syntax highlighting for the code you've posted looks o.k. in my EG 8.2 version.

Patrick_0-1591578405628.png

 

View solution in original post

6 REPLIES 6
SASKiwi
PROC Star

As a general rule you don't need to rely on the editor colour-coding to tell you if your sytax is correct or not - the colour-coding isn't always "correct". Simply just run the code and the SAS log will tell you if you have a syntax error or not. Multiple AND conditions are perfectly OK.

ballardw
Super User

There should not be any trouble because of multiple AND's in a single comparison as you show.

 

The fun part comes when mixing AND with OR. Not that it is syntactically incorrect but that many people have not studied the interaction in logical terms and get unexpected results.

 

Syntax highlighting is not the true test of the validity of the code. It's what the LOG and data results show.

ChrisNZ
Tourmaline | Level 20

@ballardw > Syntax highlighting is the true test of the validity of the code. 

You might have forgotten a word 🙂

ballardw
Super User

@ChrisNZ wrote:

@ballardw > Syntax highlighting is the true test of the validity of the code. 

You might have forgotten a word 🙂


Fixed. Thank you. I have noticed a marked increase in typos since working at a "new" "temporary" desk and I don't always catch them on the quick proof read.

Patrick
Opal | Level 21

@jaimedba 

Multiple AND are totally o.k.

Also use ELSE if there is a set of multiple related IF conditions. 

data temp.compQTR_WinRaw2a; 
  set temp.compQTR_WinRaw2;
  if missing(datacqtr) and fyr = "12" then datacqtr = datafqtr;
  else if missing(datacqtr) and fyr in ('3','4') then datacqtr = cats(fyearq, "Q1");
run;

And looking at your code guessing what you're doing: You will at one point in the rather near future also have to learn how you can work with SAS date values and formats. Once you master this working with dates in SAS will become easier.

 

Syntax highlighting for the code you've posted looks o.k. in my EG 8.2 version.

Patrick_0-1591578405628.png

 

jaimedba
Fluorite | Level 6

Thank you everyone! I am new to posting questions so I accepted one solution... yet, you all really helped!

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1547 views
  • 4 likes
  • 5 in conversation