BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5

data test;
input id age sex $;
cards;
1 25 Male
2 35 Female
3 29 Female
4 37 Male
5 32 Male
;
run;

ex1:

 

data ex1 ;

set test ;

if sex='F' then flag=1 ;

else flag=0 ;

run;

 

data ex2 ;

set test ;

flag=sex='F' ;

run;

what is the difference between ex1 and ex2

can you give some more examples

8 REPLIES 8
Patrick
Opal | Level 21

@thanikondharish

Sounds like a study question which you should try to answer yourself.

If you want support with this then may be post here your thoughts what the difference could be and ask for some feedback on your thoughts.

AndrewHowell
Moderator

@thanikondharish - have to agree with @Patrick. Try both, decide and post your reasons here.

 

While you're at it, you can also try my favourite (simplest & unambiguous):

 

flag=ifn(sex='F',1,0);
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

O I so want to reply to how the ifn works, since the same logic arguments are the same in Excel, but I should leave that to @thanikondharish.  

But I can not help myself.

first what is the conduction being tested!

second what is the value set if the conduction is true!

else what his the value set if the conduction is false!

 

this is why we create programs, test, set true else set false.

 

 

 

ChrisNZ
Tourmaline | Level 20

Just a warning about ifn().

All three function parameters (test, result true, result false) are always executed by SAS.

So C = ifn(B>0, A/B, 0)  will not work as expected and can still generate a division by zero.

Not to mention the wasted CPU cycles evaluating a value that will never be used (when the values are formulas).

 

 

AndrewHowell
Moderator

@ChrisNZ - good to know about ifn(), thanks. Wasn't aware the parameters are executed regardless.

 

CPU cycles - concur, but also balancing simplicity/complexity of maintenance with same of execution. (But will keep an eye on future implementations.)

ChrisNZ
Tourmaline | Level 20

I still like the IFx functions and use them a lot for the reason you mention, but they would be more useful if all 3 parameters were not always evaluated. This will probably never happen.

ballardw
Super User

@thanikondharish wrote:

data test;
input id age sex $;
cards;
1 25 Male
2 35 Female
3 29 Female
4 37 Male
5 32 Male
;
run;

ex1:

 

data ex1 ;

set test ;

if sex='F' then flag=1 ;

else flag=0 ;

run;

 

data ex2 ;

set test ;

flag=sex='F' ;

run;

what is the difference between ex1 and ex2

can you give some more examples


Proc compare will provide a comparison of values row by row with diagnostics about matching / nonmatching values if that is what you my by "difference between ex1 and ex2".

 

Since both sets are comparing for a value that does not exist (your sex variable is "Female" "Male" not "F" or "M") I wouldn't expect to see any matches at all.

 

Be default when you do a comparison between values SAS will yield 1 for true and 0 for false. Does that give you a hint what is going on?

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

 

If your datalines where like this

data test;
input id age sex $;
cards;
1 25 M
2 35 F
3 29 F
4 37 M
5 32 M
;
run;

there would be no difference in you code.

or if you fixed the testing to evaluate only the first char for sex then there would be no difference.

 

 

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