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

In a data call, I wrote this :

 

 

x=var1/var2;
output = ifn(x<1, 1, x, .);

 

The problem is that when var2 is missing, x is missing, but x<1 is read as TRUE and output is 1 instead of missing.

 

How can I workaround this behaviour ?

1 ACCEPTED SOLUTION

Accepted Solutions
7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26
output=ifn(x ne . and x < 1,1,x);
DanCh
Fluorite | Level 6
Thanks, it works !

But I wonder, if this implicitly set output to missing, what is the use of the third argument of ifn ?
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The condition is ifn(<logic>,result when true, result when false, result when missing)

So if you had .,1,2 in you data you might want if =1, if > 1, if missing.  It just allows to cover for all eventualities, as you might not want . if the value is missing, maybe you want to default in 0.

DanCh
Fluorite | Level 6

Since var1 is missing, x is also missing, and (x<1) is obviously missing too (else how do you know it's <1 ?). Then it seems that the first <logic> argument cannot see missing values. Else why didn't my code work ?

 

In the official doc, there is no example of third argument use, could you give me one ?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

No, . is less than 1 and missing.  So sequentially the logic is:

Is x < 1, the answer is true - stop

Is x >=1 - not evaluated

Is x missing - not evaluated

 

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002604573.htm

 

Never actually used the third option in this, and was surprised myself that there was one.

DanCh
Fluorite | Level 6

 

OK, based on your answer, I designed a little test :

 

DATA x;
y=.;
x1=y>1;
x2=y<1;
x3=y<-991;
x4=y<0;
x5=y>0;
x6=y<=0;
run;

My problem is more about the way SAS treats inequalities : it seems that missing values are always "less than" anything.

 

 

Coming from R, I find this awfuly misleading, thanks for pointing this out.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000989180.htm

May help.  It is logical that missing is a separate consideration, and should appear as a group before variables with actual data.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 992 views
  • 0 likes
  • 2 in conversation