mardi 8 novembre 2016

Using PROC SQL as an IF/ELSE IF statement

I'm trying to run the following code but it not working properly. I figured out that the problem is that each case when overwrites the next statement.

So, what I need to do is a IF/ELSE IF staetment but I don't know how to do that in PROC-SQL

proc sql;
create table example
as select *,

case when B.variable = 'X'               then 1 else 0 end as variable_X,   
case when B.variable = 'Y'               then 1 else 0 end as variable_Y,   
case when B.variable = 'Z'               then 1 else 0 end as variable_Z,   
case when B.variable = 'W'               then 1 else 0 end as variable_W,   
case when B.variable = 'R'               then 1 else 0 end as variable_R,
case when B.variable = 'G'               then 1 else 0 end as variable_G,
case when B.variable = 'T'               then 1 else 0 end as variable_T,   
case when B.variable = 'U'               then 1 else 0 end as variable_U,
case when B.variable = 'P'               then 1 else 0 end as variable_P,   
case when B.variable = 'L'               then 1 else 0 end as variable_L

FROM my_table             as A
LEFT JOIN my_second_table as B
;

I've already tried to use a group by statement but it didn't work.

P.S.: My real code is much greater than my example, with 8 left join and much more variables. I just posted an extract of it.

Aucun commentaire:

Enregistrer un commentaire