DATA Step, Macro, Functions and more

Merging data

Reply
Regular Contributor
Posts: 162

Merging data

Dear all,

 

I have 2 files:

A and B (attached).

 

I would like to merge the data by state and fyear. However, it is unknow why it failed. I used the following code:

 

PROC SQL;
CREATE TABLE TEST AS
SELECT A.*, B.*
FROM A
LEFT JOIN B
ON A.STATE=B.STATE AND A.FYEAR=B.FYEAR;
QUIT;

 

Hope anyone give me a solution.

 

Thank you.

 

MSPAK

Attachment
Super User
Posts: 19,822

Re: Merging data

Post your log and please explain what you mean by failed. 

Regular Contributor
Posts: 162

Re: Merging data

[ Edited ]

There is no error message, but the following note is found:

 

NOTE: Table WORK.test created, with 0 rows and 1262 columns.

Super User
Posts: 19,822

Re: Merging data

Check that the variables you're merging on have same type and formats - use PROC contents. 

Run a proc freq on each of the variables and look for inconsistencies. 

 

Post the log and whatever results you can. 

 

 

 

Regular Contributor
Posts: 162

Re: Merging data

Result from proc contents 

SAS Output

Alphabetic List of Variables and Attributes (DATASET A)
# Variable Type Len Label
2 BankState Char 24  
4 FailedBanknum Num 8  
3 FailureYr Num 8  
6 R5_FailedBank Num 8 Rank for Variable FailedBanknum
7 fyear Num 8  
5 logFBanknum Num 8  
1 state Char 8  

SAS Output

Alphabetic List of Variables and Attributes (DATASET B)
# Variable Type Len Format Informat Label
2 FYEAR Num 8 F6. 6. Data Year - Fiscal
1 GVKEY Char 6 $6. $6. Global Company Key
3 STATE Char 8 $8. $8. State/Province
PROC Star
Posts: 761

Re: Merging data

I get the following:

 

SNIP.PNG

 

You should probably think about what columns to select though..

Regular Contributor
Posts: 162

Re: Merging data

Hi again,

 

If you open the output, you will notice that the GVKEY variable are all blank. I need the gvkey. 

 

Super User
Posts: 19,822

Re: Merging data


mspak wrote:

There is no error message, but the following note is found:

 

NOTE: Table WORK.test created, with 0 rows and 1262 columns.


How is GVKEY blank if there's no rows. I feel like we're missing something. 

Regular Contributor
Posts: 162

Re: Merging data

If I used the following code, no row is generaed:

 

PROC SQL;
CREATE TABLE testAS
SELECT a.*, b.*
FROM B a, A b
WHERE (b.fyear = a.failureyr AND b.state = a.bankSTATE); quit:

Super User
Posts: 19,822

Re: Merging data

So you posted code in your original question and then the note from different code?

 

 

Most likely your state state aren't matching for some reason. 

Verify visually with a proc freq. Are the spellings and case exactly the same? 

Trim/Compress the variables to remove any possible invisible characters and try your merge again. 

 

 

PS Many can't/won't download zip files due to IT restrictions and viruses. 

 

Good Luck

 

 

Respected Advisor
Posts: 4,173

Re: Merging data

You get zero rows because your where clause is never true.

You should not use the * wildcard in the select statement like you do if there can be same named columns coming from both source tables.

 

And just as an observation:

Your State variable has a length of 8, your bankState variable has a length of 24. Looks to me as if these two variables contain likely different information (eg. codes or abbreviations vs full names). Also casing will be important (use an upcase() function if you can't be sure that casing matches).

Regular Contributor
Posts: 162

Re: Merging data

I can't see difference on the abrevation between two files. I might just convert it to excel and do it manually.

Ask a Question
Discussion stats
  • 11 replies
  • 336 views
  • 0 likes
  • 4 in conversation