In below program, I am trying to get same output using IFC alternative. Seems like I am missing something.
data test;
input x;
cards;
1
2
;
data test1;
set test;
if x = 1 then
a = 'A';
else if x = 2 then
a = 'B';
run;
data test2;
set test;
a=ifc(x=1,'A', ' ');
a=ifc(x=2,'B', ' ');
run;
The answer to your question depends on the possible value of x. If you know that x only takes on two values (1 and 2), then you can use
a=ifc(x=1,'A', 'B');
If x can be 1, 2, or 'other', then you could use
a=ifc(x=1,'A', ifc(x=2,'B',' '));
You can also use arrays, like this
data test1;
array AVals[3] $ _temporary_ ('A', 'B', ' ');
set test;
a = AVals[x];
run;
If you have many valid values, I suggest you look at the SELECT-WHEN statement.
When the first observation executes in data set test2, x=1 and a gets assigned the value of A, it then moves on to the second command and still x=1 which causes a to get assigned a missing value.
@PaigeMiller Thank you for the explanation. How to get the desired output using IFC if possible?
Personally, I wouldn't even try to fit this example into an IFC (or several IFC) commands, but I'm sure someone will come along with a brilliant solution.
The answer to your question depends on the possible value of x. If you know that x only takes on two values (1 and 2), then you can use
a=ifc(x=1,'A', 'B');
If x can be 1, 2, or 'other', then you could use
a=ifc(x=1,'A', ifc(x=2,'B',' '));
You can also use arrays, like this
data test1;
array AVals[3] $ _temporary_ ('A', 'B', ' ');
set test;
a = AVals[x];
run;
If you have many valid values, I suggest you look at the SELECT-WHEN statement.
I like nested ifc() functions beacuse they are easily the most legible, if a bit wasteful; One knows exactly what the code does just by glancing at it.
data ;
A1=ifc(B<10 , 'A'
,ifc(B<20 , 'B'
,ifc(B<20 & C , 'C'
,ifc(B<20 , 'D'
,ifc(B<20 , 'E'
, 'F' )))));
A2=ifc(B1<10 , 'A'
,ifc(B1<20 , 'B'
,ifc(B1<20 & C , 'C'
,ifc(B1<20 , 'D'
,ifc(B1<20 , 'E'
, 'F' )))));
run;
Now if only SAS didn't try to resolve all arguments all the time, and instead treated this function differently to other functions, we could also do this:
data ;
A=0;
B=ifc(A>0, log(A), .);
run;
@ChrisNZ wrote:
I like nested ifc() functions beacuse they are easily the most legible, if a bit wasteful; One knows exactly what the code does just by glancing at it.
data ; A1=ifc(B<10 , 'A' ,ifc(B<20 , 'B' ,ifc(B<20 & C , 'C' ,ifc(B<20 , 'D' ,ifc(B<20 , 'E' , 'F' ))))); A2=ifc(B1<10 , 'A' ,ifc(B1<20 , 'B' ,ifc(B1<20 & C , 'C' ,ifc(B1<20 , 'D' ,ifc(B1<20 , 'E' , 'F' ))))); run;
Legibility is in the eye of the beholder. I consider this illegible.
>Legibility is in the eye of the beholder.
Indeed, so it seems. 🙂
@Rick_SAS already answered your question. However, in this case, I wouldn't use the ifc function. I'd use:
data test1; set test; a=byte(x+64); run;
Art, CEO, AnalystFinder.com
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.