BookmarkSubscribeRSS Feed
fpascal
Quartz | Level 8

How do I see if records with matching keys are in both files and then output only the records that are in the second file into a new file?

 

proc sql;
create table cohort2 as
  select *
     from etl.inp_claims_lds2013_2014_lejr etl.inp_claims_lds2015_2016_lejr
       where etl.inp_claims_lds2015_2016_lejr.desy_sort_key in (select
        desy_sort_key from etl.inp_claims_lds2013_2014_lejr);
quit;

9 REPLIES 9
novinosrin
Tourmaline | Level 20

What do you think is wrong with your code. Seems ok or am i not understanding your description?

fpascal
Quartz | Level 8
75
76 * Log/List Options *;
77 options ls=120 ps=42 missing=' ' nocenter validvarname=upcase compress=binary;
78 * Output SAS Library *;
79 libname etl 'D:\Medicare\05_etl_output\';
NOTE: Libref ETL was successfully assigned as follows:
Engine: V9
Physical Name: D:\Medicare\05_etl_output
80
81
82 proc sql;
83 create table cohort2 as
84 select *
85 from etl.inp_claims_lds2013_2014_lejr etl.inp_claims_lds2015_2016_lejr
_
22
200
ERROR 22-322: Syntax error, expecting one of the following: ;, ',', ANSIMISS, CROSS, EXCEPT, FULL, GROUP, HAVING,
INNER, INTERSECT, JOIN, LEFT, NATURAL, NOMISS, ORDER, OUTER, RIGHT, UNION, WHERE.

ERROR 200-322: The symbol is not recognized and will be ignored.

86 where etl.inp_claims_lds2015_2016_lejr.desy_sort_key in (select
_
22
76
ERROR 22-322: Syntax error, expecting one of the following: ;, !, !!, &, *, **, +, -, /, <, <=, <>, =, >, >=, ?, AND,
BETWEEN, CONTAINS, EQ, EQT, EXCEPT, GE, GET, GROUP, GT, GTT, HAVING, IN, INTERSECT, IS, LE, LET, LIKE,
LT, LTT, NE, NET, NOT, NOTIN, OR, ORDER, OUTER, UNION, ^, ^=, |, ||, ~, ~=.

ERROR 76-322: Syntax error, statement will be ignored.

87 desy_sort_key from etl.inp_claims_lds2013_2014_lejr);
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
88 quit;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.04 seconds
cpu time 0.01 seconds

novinosrin
Tourmaline | Level 20

my tired eyes didn't capture the missing comma in from clause

 

from etl.inp_claims_lds2013_2014_lejr   ,    etl.inp_claims_lds2015_2016_lejr

 

check that. 

 

also, at late evening here don't trust my eyes

fpascal
Quartz | Level 8
Almost there


80
81
82 proc sql;
83 create table cohort2 as
84 select *
85 from etl.inp_claims_lds2013_2014_lejr, etl.inp_claims_lds2015_2016_lejr
86 where etl.inp_claims_lds2015_2016_lejr.desy_sort_key in (select
_
22
76
ERROR 22-322: Syntax error, expecting one of the following: ;, !, !!, &, *, **, +, -, /, <, <=, <>, =, >, >=, ?, AND,
BETWEEN, CONTAINS, EQ, EQT, EXCEPT, GE, GET, GROUP, GT, GTT, HAVING, IN, INTERSECT, IS, LE, LET, LIKE,
LT, LTT, NE, NET, NOT, NOTIN, OR, ORDER, OUTER, UNION, ^, ^=, |, ||, ~, ~=.

ERROR 76-322: Syntax error, statement will be ignored.

87 desy_sort_key from etl.inp_claims_lds2013_2014_lejr);
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
88 quit;

novinosrin
Tourmaline | Level 20

what's your objective

 

Are you after this logic?

 

proc sql;
select *
from b
where key in (select key from a);
quit;

fpascal
Quartz | Level 8
I want to take check each record in file a to see if it is in file b. If so I want to add it to a new file c.
novinosrin
Tourmaline | Level 20

You are better off posting a sample of your data and the requirement

PGStats
Opal | Level 21

SAS/SQL doesn't support 3-level variable names. You must use aliasses :

 

from etl.inp_claims_lds2013_2014_lejr as a, etl.inp_claims_lds2015_2016_lejr as b
       where b.desy_sort_key in (select
        desy_sort_key from etl.inp_claims_lds2013_2014_lejr)
PG
fpascal
Quartz | Level 8

thanks - below it states it cannot find my files but they are in library ETL.  Is there something else i need to add

 

 

73 libname etl 'D:\Medicare\05_etl_output\';
NOTE: Libref ETL was successfully assigned as follows:
Engine: V9
Physical Name: D:\Medicare\05_etl_output
74
75 proc sql;
76 create table cohort as
77 select *
78 from etl.inp_claims_2013_2014_lejr as a, etl.inp_claims_2015_2016_lejr as b
79 where b.desy_sort_key in (select
80 desy_sort_key from a);
ERROR: File ETL.INP_CLAIMS_2013_2014_LEJR.DATA does not exist.
ERROR: File ETL.INP_CLAIMS_2015_2016_LEJR.DATA does not exist.
ERROR: File WORK.A.DATA does not exist.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
81 quit;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 9 replies
  • 1413 views
  • 0 likes
  • 3 in conversation