BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

 

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
SAS_inquisitive
Lapis Lazuli | Level 10

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

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Rick_SAS
SAS Super FREQ

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.

ChrisNZ
Tourmaline | Level 20

 

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;

 

 

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
ChrisNZ
Tourmaline | Level 20

>Legibility is in the eye of the beholder.

 

Indeed, so it seems.  🙂

art297
Opal | Level 21

@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

 

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
  • 8 replies
  • 1196 views
  • 4 likes
  • 5 in conversation