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

can someone please help me with this???

 

Example codes

data test;
input x $1;
z=ifc(x='M','zero','one','missing');
datalines;
M
F

M
F
;
run;

data test1;
input x $1;
z=ifn(x='M',0,1,.);
datalines;
M
F

M
F
;
run;

proc print data=test;
run;
proc print data=test1;
run;

Result:

 

nbav001_0-1678676092953.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
nbav001
Fluorite | Level 6

Thank you everyone for your replies and suggestions. 

I tried the following code and it worked for me.

nbav001_0-1678754818788.png

 

View solution in original post

9 REPLIES 9
Tom
Super User Tom
Super User

SAS does not do TRI-LEVEL logic. SAS only does BOOLEAN logic.  The value of X is either 'M' or it isn't. There is no third option.  

 

If you want to treat X=' ' as different than other non 'M' values then code it that way. Also why bother using IFC() or IFN() when you can use normal IF/THEN statements?

data test;
  input x $1.;
  length z $7 ;
  if x='M' then z='zero';
  else if missing(x) then z='missing';
  else z='one';
datalines;
M
F

M
F
;

Tom_0-1678676469272.png

 

 

 

nbav001
Fluorite | Level 6

Reference1: https://www.lexjansen.com/wuss/2012/28.pdf

Reference2: https://documentation.sas.com/doc/en/vdmmlcdc/8.1/lefunctionsref/n0l3n5z2h31h7wn1fmnqd33ibhap.htm

Reference3: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0l3n5z2h31h7wn1fmnqd33ibhap.h...

According to the above documentation there is a 4th optional argument that can is used for IFN and IFC for missing values. 

But I my case it is not working. 

SASKiwi
PROC Star

You are misinterpreting the documentation. The logical expression "x='M'"  can only be true or false, never missing. However if you do a calculation that produces a missing value then the 4th parameter applies:

data _null_;
  age = .;
  result = ifc (age * 10, 'Age times 10','Age not times 10','blank');
  put _all_;
run;

 

Tom
Super User Tom
Super User

@nbav001 wrote:

Reference1: https://www.lexjansen.com/wuss/2012/28.pdf

Reference2: https://documentation.sas.com/doc/en/vdmmlcdc/8.1/lefunctionsref/n0l3n5z2h31h7wn1fmnqd33ibhap.htm

Reference3: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0l3n5z2h31h7wn1fmnqd33ibhap.h...

According to the above documentation there is a 4th optional argument that can is used for IFN and IFC for missing values. 

But I my case it is not working. 


So the documentation you referenced is confusing because of the use of "logical-expression" as the name of the first argument.  SAS does not do tri-level logic so a test like A=B or X<Y can only ever evaluate to 1 (TRUE) or 0 (FALSE).  So if you actually code a boolean expression in that argument it will never have a missing value.  The only way to pass a missing value is to pass a value or an arithmetic expression.

ballardw
Super User

It might help to describe exactly what you are attempting to do instead of showing code that doesn't work.

 

If the object is to display a different value than that currently assigned to variable the basic tool might be a format. If you want to create a numeric value from text then an informat with input.

 

proc format;
value $mf
'M'='Zero'
'F'='One'
' '='Missing'
;
invalue mf2num
'M'=0
'F'=1
' '=.
;

data test;
input x $1.;
length z $ 8.;
z= put(x,mf.);
y= input(x,mf2num.);
datalines;
M
F

M
F
;

With a Format to display existing values then you don't even need to create a new variable, just us Format X $mf.; where you want to see the text.

s_lassen
Meteorite | Level 14

What the IFC function does: evaluates an expression. If the expression evaluates to a number that is logical TRUE (in SAS, that is any number which is not zero or missing), it returns the second parameter, if the expression returns a zero, the function returns the third parmeter, and if the expression is missing, it will return the fourth parameter. If there is no fourth parameter, the function will return the third parameter for missing values also.

e.g.:

67   data _null_;
68     input x;
69     a=ifc(x,'a','b');
70     b=ifc(x,'a','b','c');
71     put x= a= b=;
72   cards;

x=. a=b b=c
x=0 a=b b=b
x=1 a=a b=a
x=-232352 a=a b=a

And as @Tom remarked, you may as well use normal IF..THEN..ELSE logic. It may be a bit more typing, but it is easier to read. And if you use IFC or IFN with more complicated output parameters, you should be aware that all the possible outputs will be calculated, which may require more CPU time.

 

But this last fact is also why IFC/IFN are often used with LAG and DIFF expressions. For instance this will probably not work as intended:

if missing(a) then
  a=lag(a);

because the LAG function will only store a value when A is missing. But this

a=ifc(missing(a),lag(a),a);

will work because the LAG function will be called every time.

 

nbav001
Fluorite | Level 6

Thank you everyone for your replies and suggestions. 

I tried the following code and it worked for me.

nbav001_0-1678754818788.png

 

ballardw
Super User

@nbav001 wrote:

Thank you everyone for your replies and suggestions. 

I tried the following code and it worked for me.

nbav001_0-1678754818788.png

 


I think that you might also want to consider a case where you have code values A B C D E F and G that are supposed to become 0, 1, 2, 3, 4, 5, 6 and 7 respectively with missing if the code is not present. Write that out with IFN and IFC. I suspect you will find out why I suggested the Informat approach.

nbav001
Fluorite | Level 6

Yes, I understand that. I also agree that using informants or other options such as using select when or if else are simple and easily understandable. Thank you for  your suggestion. 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 9 replies
  • 1860 views
  • 1 like
  • 5 in conversation