BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
accintron
Obsidian | Level 7

I'm trying to clean up my code and wondering if there is a simpler way to list the REFERER_SOURCE variables instead of listing them all out since they have the same prefix? I know you can reference variables like REFERER_SOURCE_1-REFERER_SOURCE_7 in other commands, but struggling to figure this out with the left join. THanks!

 

proc sql;
create table adms_4 as
select a.*, b.REFERER_SOURCE_1, b.REFERER_SOURCE_2, b.REFERER_SOURCE_3, b.REFERER_SOURCE_4, b.REFERER_SOURCE_5, b.REFERER_SOURCE_6, b.REFERER_SOURCE_7
from adms_3 a left join RS2 b on a.ADMISSION_ID = b.ADMISSION_ID;
quit;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Are you doing a many-to-many merge?

If not then just use normal SAS code instead of SQL and then you can use variable lists in many places.

For this example you can probably just use a KEEP= option on the RS2 input dataset in either SQL or normal code. 

data adms_4;
  merge   adms_3(in=in_a) RS2 (keep=admission_id referer_source_: );
  by ADMISSION_ID;
  if in_a;
run;

In SQL you might want to also rename the duplicate ADMISSION_ID column so that you can use * in your column list without getting warnings/notes about duplicate names. You can then DROP it from the output dataset.

create table adms_4(drop=id_B) as
  select * 
  from adms_3 a 
  left join RS2(keep=admission_id referer_source_: rename=(admission_id=id_B)) b 
  on a.ADMISSION_ID = b.id_B
;

 

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

SAS/SQL syntax doesn't provide variable list shortcuts. But dataset options do. In some circumstances you can request all variables

 

... select b.*, ...

 

and exclude unwanted columns using dataset option drop on input

 

... from myData(drop=junk1) as b ...

 

or on output

 

create table want(drop=junk10) as ...

 

or include only wanted columns with dataset option keep

 

... left join RS2(keep=ADMISSION_ID REFERER_SOURCE_:  )

 

PG
PaigeMiller
Diamond | Level 26

Or do a DATA step merge, where you can abbreviate the list of variables desired to REFERER_SOURCE_:

--
Paige Miller
Tom
Super User Tom
Super User

Are you doing a many-to-many merge?

If not then just use normal SAS code instead of SQL and then you can use variable lists in many places.

For this example you can probably just use a KEEP= option on the RS2 input dataset in either SQL or normal code. 

data adms_4;
  merge   adms_3(in=in_a) RS2 (keep=admission_id referer_source_: );
  by ADMISSION_ID;
  if in_a;
run;

In SQL you might want to also rename the duplicate ADMISSION_ID column so that you can use * in your column list without getting warnings/notes about duplicate names. You can then DROP it from the output dataset.

create table adms_4(drop=id_B) as
  select * 
  from adms_3 a 
  left join RS2(keep=admission_id referer_source_: rename=(admission_id=id_B)) b 
  on a.ADMISSION_ID = b.id_B
;

 

accintron
Obsidian | Level 7

Ah, thank you! Not doing many to many merge. These suggestions are so helpful and straightforward . I appreciate it!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 7637 views
  • 0 likes
  • 4 in conversation