BookmarkSubscribeRSS Feed
imdickson
Quartz | Level 8

Hi SAS Community. I am trying to convert PROC SQL statement with some columns having CASE WHEN statement into Data step hash object lookup for neater looking and some other factors set by my team.

 

Here is the challenge that I am facing. There are some CASE WHEN statement that is actually taking from multiple sources. For example:

proc sql;

create table a as

select 

case when type eq 'PMU' then Lng=ppp.Lng

when type eq 'PPU' then Lng=yyy.Lng

end as Lng

from b left join c

on b.Id=c.Id;

;

quit;

 

 

My Data Step Hash Object lookup will look something like this without the CASE WHEN statement, which i want to include in but dont know how.

Data SimResultCapLoad;
	
  if _n_=1 then do;
     declare hash h(dataset:"c)");
     h.definekey('Id');
     h.definedata('Lng');
     h.definedone();

  end;
  set netcap.SimResultCapLoad;

  *Lookup;
  rc=h.find();

run;

But how do i add the checking of CASE WHEN in?

6 REPLIES 6
andreas_lds
Jade | Level 19

Both steps you posted are not error-free and are using different datasets and variables, please fix these problems.

 

In a data step select/case would be replaced by select/when. The left join can be written as merge with by and in.

 

 

Tom
Super User Tom
Super User

You need to provide more details about what you are trying to do and include some simple example datasets to make it clearer.

From your brief explanation you probably just want to create formats from your different lookup tables.

Then you can use the PUTN() function to dynamically pick which format to use.

Even easier if you make the format names match the value of the TYPE variable.

data A;
  set B;
  LNG = putn(id,cats(type,'.'));
run;

If your ID variable is character then use PUTC() function instead and add $ prefix on the format names. 

SASKiwi
PROC Star

Personally I don't see any point in switching to hash look-ups unless you are gaining a clear functional or performance advantage.

 

@Tom 's look-up format approach is way easier to code and is as fast as hash look-ups in many circumstances.

 

I have DATA steps doing 30 plus format look-ups. There is no way you would ever want to code those using hashes unless you enjoy writing 10 times the amount of code. I also (very rarely) use hash but only when there is a clear advantage in doing so.

 

 

 

Patrick
Opal | Level 21

@imdickson 

In a data step you'd address the CASE/WHEN syntax with an IF/THEN - and you lookup/assign values based of the outcome of this condition.

 

The (syntactically incorrect) SQL you've posted has a left join in it. So here if the relationship between the tables is 1:n with the table to the left having the n matching rows then a simple hash lookup won't do anymore but you need also to loop using the hash do_over method.

imdickson
Quartz | Level 8

Hi everyone. I've figured out the way to do it. What i need to do is to add "If then else" condition after "a.find()" statement.

 

It is now working fine. Thanks everyone for trying to help me out there. Cheers.

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
  • 6 replies
  • 806 views
  • 1 like
  • 6 in conversation