BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
braam
Quartz | Level 8

In the below code, ifn function doesn't work as I expected. Can I assign missing (.) to HigherThan20 if mpg_city is missing?

I would like to use PROC SQL for this, though I know there is a way to do so when using data step.

 

 


data temp;
	set sashelp.cars;
	if mpg_city= 22 then mpg_city= .; run;

proc sql;
	create table temp
	as select a.type, a.mpg_city, ifn(mpg_city>20, 1, 0, .) as HigherThan20
	from temp a; quit;
proc print data= temp(obs= 20); run;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @braam  You need a CASE WHE construct and here is a traditional way of writing a CASE WHEN THEN ELSE END construct

 

data temp;
	set sashelp.cars;
	if mpg_city= 22 then mpg_city= .; 
run;

proc sql;
create table temp_want as 
select a.type, a.mpg_city,case when  mpg_city>20 then 1 
when  .< mpg_city<=20 then 0 else . end as  HigherThan20
from temp a; 
quit;

proc print noobs;
run;

View solution in original post

4 REPLIES 4
Krueger
Pyrite | Level 9

The problem is your logical expression results in a true/false regardless if mpg_city is missing or not. In this case it returns as false. 

 

I'm unsure if this is the correct or best approach but it's the first solution that came to mind.

 

proc sql;
	create table temp1
	as select a.type, a.mpg_city, ifn(mpg_city>20, 1, ifn(mpg_city is missing, ., 0, 0), .) as HigherThan20
	from temp a; quit;

 

I do think a CASE statement would be more appropriate here but I come from a SQL background.  

Tom
Super User Tom
Super User
proc sql;
select type, mpg_city
,case when (not missing(mpg_city)) then (mpg_city > 20) end as HigherThan20
from temp 
; 
quit;

Not sure why you want to use IFN() if you want to use SQL.  Use CASE instead.

Boolean expressions return 1 when true and 0 when false. They never return a missing value. You will have to test for missing with your own code. 

novinosrin
Tourmaline | Level 20

Hi @braam  You need a CASE WHE construct and here is a traditional way of writing a CASE WHEN THEN ELSE END construct

 

data temp;
	set sashelp.cars;
	if mpg_city= 22 then mpg_city= .; 
run;

proc sql;
create table temp_want as 
select a.type, a.mpg_city,case when  mpg_city>20 then 1 
when  .< mpg_city<=20 then 0 else . end as  HigherThan20
from temp a; 
quit;

proc print noobs;
run;
ChrisNZ
Tourmaline | Level 20

>  ifn(mpg_city>20, 1, 0, .) as HigherThan20

 

You are making up the function's syntax as you go, aren't you? 

This isn't Excel. Read the documentation.

 

Does this do what you want?

ifn( missing(MPG_CITY), . , MPG_CITY>20 )

 

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