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?

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