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

 


proc sql; create table mr as select z.gvkey, z.fyear, a.mr13, a.rawret13, b.mr16, b.rawret16, c.mr24, c.rawret24, d.mr25, d.rawret25, e.mr28, e.rawret28, f.mr37, f.rawret37, g.mr40, g.rawret40 from esgprice z, mr13 a, mr16 b, mr24 c, mr25 d, mr28 e, mr37 f, mr40 g where z.gvkey = a.gvkey and z.fyear = a.fyear and z.gvkey = b.gvkey and z.fyear = b.fyear and z.gvkey = c.gvkey and z.fyear = c.fyear and z.gvkey = d.gvkey and z.fyear = d.fyear and z.gvkey = e.gvkey and z.fyear = e.fyear and z.gvkey = f.gvkey and z.fyear = f.fyear and z.gvkey = g.gvkey and z.fyear = g.fyear; quit;

Hi all!

 

 

I referred to this link: https://communities.sas.com/t5/SAS-Programming/Merge-Multiple-tables/td-p/384279

to merge 8 tables and, again, in trouble.

 

1. Instead of what I wanted, around 6000 values, I obtained more than a million values due to duplicates. 

It looks that I used many gvkey=gvkey and fyear=fyear. I thought SAS may consider them as group by FYEAR and GVKEY but didn't.

As I see in one table-on-one table merge I assumed that AND may mean each gvkey in each year. I just tried group by ~~, ~~;

Yes.  I got this error code; ERROR 180-322: Statement is not valid or it is used out of proper order.

 

So... how can I get the result without duplicates? I simply want to have the outcome as I do with one table by one table.

 

2. I have a lot of variables in Z table and writing every single item in the above code is somewhat looking like dumb. I guess there must be a short cut to put all variables in my Z table. Do you know that kind of code? 

 

Somehow I need to finish my summer project but after that I have to take some SAS class at Udemy or whatever online courses...

 

Thank you very much for your help

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why are you using SQL if you just want to do a normal merge?

data mr;
  merge  mr13 mr16 mr24 mr25 mr28 mr37 mr40;
  by gvkey fyear;
run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Why are you using SQL if you just want to do a normal merge?

data mr;
  merge  mr13 mr16 mr24 mr25 mr28 mr37 mr40;
  by gvkey fyear;
run;
Kurt_Bremser
Super User

Maxim 3: Know Your Data.

Maxim 3 implies that you also need to know relationships between datasets, as that is the absolutely necessary basis for writing correct merge or join code. If you find that you do not have a one-to-one or one-to-many relationship, but have to deal with a many-to-many relationship, you need to decide:

  • do a cartesian join (that's what you get with SQL, as you experienced)
  • create intermediary summary tables (for a data step one-to-one or one-to-many merge)
  • use grouping and summing in SQL
  • or is it possible to put the data side-by-side anyway without losing information (classic data step merge)

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