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.
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.
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.
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.
@ballardw > Syntax highlighting is the true test of the validity of the code.
You might have forgotten a word 🙂
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.
Thank you everyone! I am new to posting questions so I accepted one solution... yet, you all really helped!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.