BookmarkSubscribeRSS Feed
Sara1
Calcite | Level 5

How to do -- Case when then select ( column A from table A) else select column b from table b. 

Thanks 

4 REPLIES 4
Tom
Super User Tom
Super User

When WHAT?  How are you joining table A and table B so that both available to reference the CASE?

select 
  case when ( ????) then A.A else B.B end as A_or_B
   from A , B
;

Or are you taling about generating code based on some external condition.  So perhaps the value of some macro variable?  If so then use macro logic to conditionally generate the statement you want to create.

%if &XX= OPTION1 %then %do;
select A from A;
%end;
%else %do;
select B from B;
%end;
supp
Pyrite | Level 9

Is this what you are trying to do?

 

proc sql;
    select case when <put logic here> then a.column_A else b.column_B end as new_column
    from table_A as a
    join table_B as b on <put join here>;
quit;
Krueger
Pyrite | Level 9
SELECT CASE WHEN A.ID = X THEN A.COLUMN
            ELSE B.COLUMN
        END AS NAME
  FROM TableA A
  JOIN TableB B on A.ID = B.ID
Reeza
Super User
It depends on the rest of the query - COALESECE() may be an option for example. Is table B already in the query somewhere?

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1246 views
  • 0 likes
  • 5 in conversation