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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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