DATA Step, Macro, Functions and more

ifc

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

ifc

 

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;

 

 


Accepted Solutions
Solution
‎06-15-2017 06:36 PM
SAS Super FREQ
Posts: 3,752

Re: ifc

[ Edited ]
Posted in reply to SAS_inquisitive

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.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,913

Re: ifc

Posted in reply to SAS_inquisitive

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.

Super Contributor
Posts: 271

Re: ifc

Posted in reply to PaigeMiller

@PaigeMiller  Thank you for the explanation. How to get the desired output using IFC if possible?

Trusted Advisor
Posts: 1,913

Re: ifc

Posted in reply to SAS_inquisitive

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.

Solution
‎06-15-2017 06:36 PM
SAS Super FREQ
Posts: 3,752

Re: ifc

[ Edited ]
Posted in reply to SAS_inquisitive

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.

PROC Star
Posts: 1,759

Re: ifc

 

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;

 

 

Trusted Advisor
Posts: 1,913

Re: ifc


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.

PROC Star
Posts: 1,759

Re: ifc

Posted in reply to PaigeMiller

>Legibility is in the eye of the beholder.

 

Indeed, so it seems.  Smiley Happy

PROC Star
Posts: 7,471

Re: ifc

Posted in reply to SAS_inquisitive

@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

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 243 views
  • 4 likes
  • 5 in conversation