BookmarkSubscribeRSS Feed
SAS_inquisitive
Lapis Lazuli | Level 10

Hello,

 

Does this data step code represent sql equivalent below?

 

data want;

   merge a (in = a) b (in = b) c (in = c);

   by id;

   if a or b;

run;

 

proc sql;

   create table want as

   select a*,b*,c*

     from a

        left join b on a.id = b.id

        left join c on b.id = c.id;

quit;

 

 

 

3 REPLIES 3
art297
Opal | Level 21

No. If it isn't a many to many merge/join then I think the following would be the same:

 

data want1;
   merge a (in = a) b (in = b) c (in = c);
   by id;
   if a and (a or b);
run;
 
proc sql;
   create table want2 as
   select a.*,b.*,c.*
     from a
        left join b on a.id = b.id
        left join c on b.id = c.id
  ;
quit;

HTH,

Art, CEO, AnalystFinder.com

 

mkeintz
PROC Star

The short answer is no.  But SAS is a data analysis system, and your question is an excellent opportunity to use it. Here is such a test:

 

data a b c;

  do id=1 to 2;

    output a b c;

    output b c;

    output c;

  end;

run;

data mergeabc;

  merge a b c;

  by id;

run;

 

proc sql;

  create table want as

  select a.*,b.*,c.*

  from a

    left join b on a.id = b.id

    left join c on b.id = c.id;

quit;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
LinusH
Tourmaline | Level 20

Why do you ask?

And, strictly, you never be sure if a SQL query and a data step have the same result. It all comes down to the data at hand, and what your desired outcome is. SO I think the general answer to all SQL vs data step question should be no. On of the most obvious things is that SQL does not preserve the original sort order.

Data never sleeps

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 5579 views
  • 2 likes
  • 4 in conversation