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

Hello,

I am working on a few else if conditions to understand how else if works on different variables:

In the below example 'cl' doesn't get overwritten to 2 which means once the if condition is successful on a record it is not evaluating that record again.

 

data cls;
set sashelp.class;
if sex="M" then cl=1;
else if age=14 then cl=2;
run;

 

However, in the below example where I am working on same record and running else if on two different variables, it is getting overwritten. I don't understand why it is reading the same record again in else if (it did not in the above example).

 

Tommer_0-1668188339600.png

 

data chk;
set cm1;

if (trtsdtm <= aendtm) then cmfl= 'Y';
else if (trtsdt <= aendt) then cmfl= 'Y';
run;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@Tommer wrote:
For the sashelp.class, it did not seem to do the same thing---cl is populated as 1 not 2 although the second condition is true as well.

That's how IF THEN ELSE works ... when the IF is true on a given record, the ELSE doesn't get applied to that record.

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

I don't understand why it is reading the same record again in else if (it did not in the above example).

 

A SAS data step works on all records. It applies all the code to all records (unless there is some code to force it to advance to another record).

 

So, this section of code comprising two lines, one beginning with IF and the nnext one beginning with ELSE, are executed on the current record,

 

 

if (trtsdtm <= aendtm) then cmfl= 'Y';
else if (trtsdt <= aendt) then cmfl= 'Y'; 

 

 

as there is nothing in your code to tell it to move on the the next record between records. If either or both conditions are true, you get CMFL='Y'.

 

The code you show working on SASHELP.CLASS does the same thing. The block of code comprising of an IF and an ELSE are run on all records.

 

--
Paige Miller
Tommer
Obsidian | Level 7
For the sashelp.class, it did not seem to do the same thing---cl is populated as 1 not 2 although the second condition is true as well.
PaigeMiller
Diamond | Level 26

@Tommer wrote:
For the sashelp.class, it did not seem to do the same thing---cl is populated as 1 not 2 although the second condition is true as well.

That's how IF THEN ELSE works ... when the IF is true on a given record, the ELSE doesn't get applied to that record.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 738 views
  • 0 likes
  • 2 in conversation