DATA Step, Macro, Functions and more

proc sql left join with case when created variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 112
Accepted Solution

proc sql left join with case when created variable

dear community,

          I am trying to join 3 tables using proc sql and i have to use a variable (field) that I created with case when condition (as variable1). I need to merge this new variable1 to the 3rd table and sas is throughing off an error that it can't locate the variable1 (as it is created and not there on the 3 datasets). How can I join using the newly created variable? 

eg;

proc sql;

create table new as 

select a.*, case when  (a.statuses=' ') and (a.reasons='') then a.check
 when a.statuses =' ' then a.check_bp
 else a.statuses end as current_status,b.height,c.weight

from one as a left join two as b

         on (a.id=b.id) 

         left join three as c

            on (current_status=c.status); quit;

ERROR: The following columns were not found in the contributing tables: current_status.

 

 

thanks in advance 

 

 

 

 


Accepted Solutions
Solution
‎12-15-2017 12:40 AM
Frequent Contributor
Posts: 112

Re: proc sql left join with case when created variable

thank you LinusH for replying. It didn't work with calculated. I had to create a view with another select statement and pick it from there to join the tables. 

 

 

proc sql;

create view v_new as 

select case when  (a.statuses=' ') and (a.reasons='') then a.check
 when a.statuses =' ' then a.check_bp
 else a.statuses end as current_status,id

from one a; 

create table new as 

select a.*, v.current_status,b.height,c.weight

from one as a left join two as b

         on (a.id=b.id) 

          left join v_new as v

             on (b.id=v.id)

         left join three as c

            on (v.current_status=c.status); quit;

 

 

thanks,

 

View solution in original post


All Replies
Super User
Posts: 5,829

Re: proc sql left join with case when created variable

Try to use the CALCULATED key word.
Data never sleeps
Solution
‎12-15-2017 12:40 AM
Frequent Contributor
Posts: 112

Re: proc sql left join with case when created variable

thank you LinusH for replying. It didn't work with calculated. I had to create a view with another select statement and pick it from there to join the tables. 

 

 

proc sql;

create view v_new as 

select case when  (a.statuses=' ') and (a.reasons='') then a.check
 when a.statuses =' ' then a.check_bp
 else a.statuses end as current_status,id

from one a; 

create table new as 

select a.*, v.current_status,b.height,c.weight

from one as a left join two as b

         on (a.id=b.id) 

          left join v_new as v

             on (b.id=v.id)

         left join three as c

            on (v.current_status=c.status); quit;

 

 

thanks,

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 453 views
  • 2 likes
  • 2 in conversation