Desktop productivity for business analysts and programmers

How to Merge / Combine two tables using PROC SQL or Data Merge or Combine

Reply
Frequent Contributor
Posts: 87

How to Merge / Combine two tables using PROC SQL or Data Merge or Combine

[ Edited ]

I have the following table_1 :

 

TPMCPWCPWSCSiteETDateTimeDIAMPXMCSF
71017101US0005211Lathing08Nov201611:58890.3 1
71027102US0003611Lathing02Nov201613:01878.1 1
71027102UC0003482Lathing07Nov201618:22877.3 1
71067106UC004241Lathing05Oct20169:43890,4 1
71067106UC004373Lathing07Nov201618:23877.1 1
71067106UC3094Lathing07Nov201618:26877.8 1
71077107UC053271Lathing06Oct20168:41837 1
71077107UC2002Lathing13Oct201612:53890.55 1
71087108UC0003613Lathing02Nov201613:01878.1 1
71087108UC004321Lathing07Nov201618:25877.8 1
71087108UC1062Lathing03Oct20169:37890.3 1

 

 

 

and table_2 :

 

TPMCPWCPWSCSiteETDateTimeDIAMPXMCSF
7101....01JAN16....
7101....02JAN16....
..........
..........
..........
7101....30DEC16....
7101....31DEC16....
7102....01JAN16....
7102....02JAN16....
..........
..........
..........
7102....30DEC16....
7102....31DEC16....

 

 

 

I want to merge two tables in a way that the output should look like something:

TPMCPWCPWSCSiteETDateTimeDIAMPXMCSF
7101....01JAN16....
7101....02JAN16....
..........
71017101US0005211Lathing08Nov201611:58890.3 1
..........
..........
7101....30DEC16....
7101....31DEC16....
7102....01JAN16....
7102....02JAN16....
..........
71027102US0003611Lathing02Nov201613:01878.1 1
71027102UC0003482Lathing07Nov201618:22877.3 1
..........
..........
7102....30DEC16....
7102....31DEC16....

 

 

How can it be done using 'Proc SQL' or 'Data Merge' or 'Combine'?

 

I want to learn all three methods for future use.

 

 

Frequent Contributor
Posts: 87

Re: How to Merge / Combine two tables using PROC SQL or Data Merge or Combine

[ Edited ]
Posted in reply to imanojkumar1

In the simplest form I used:

 

data data_set;
	set table_1 table_2;
run;

But this produced duplicate values of dates. For example:

 

 

TPMCPWCETPWSCSiteDateTimeDIAMPXMCSF
7618...129SEP2016 ...
7618..UC00424230SEP2016 ...
7618.LathingUC00437130SEP201617:15890.500000..
7618.LathingUC309230SEP201620:32890.500000..
7618...301OCT2016 ...
7618...102OCT2016 ...

 

 

 

I don't know how can I avoid this. I do not want rows where there is no 'ET' (i.e. ET is '.' or empty, I do not want those rows).

 

So in the final table there will not be the line which is 'BOLD' in above output sample. And similarly, all other 'such rows', in final output table, will not be there as well.

Super User
Posts: 10,848

Re: How to Merge / Combine two tables using PROC SQL or Data Merge or Combine

Posted in reply to imanojkumar1
So ET is always missing in Table2?

data data_set;
	set table_1 table_2;
       by TPMC Date;
      if first.Date;
run;


Frequent Contributor
Posts: 87

Re: How to Merge / Combine two tables using PROC SQL or Data Merge or Combine

Thanks for helping.

Yes second table has only missing data. but may be later in future table 2 might have data. as of now, it is missing only.

Super User
Posts: 10,534

Re: How to Merge / Combine two tables using PROC SQL or Data Merge or Combine

Posted in reply to imanojkumar1

Simply add

if et not in ("."," ");

after the set statement.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 87

Re: How to Merge / Combine two tables using PROC SQL or Data Merge or Combine

Posted in reply to KurtBremser

This will return only a table which is similar like table1. all of table2 rows are not added.

Super User
Posts: 10,534

Re: How to Merge / Combine two tables using PROC SQL or Data Merge or Combine

Posted in reply to imanojkumar1

imanojkumar1 wrote:

This will return only a table which is similar like table1. all of table2 rows are not added.


If ET is always missing in table2, then this is clearly a consequence of your requirement, quote:

"I do not want rows where there is no 'ET' (i.e. ET is '.' or empty, I do not want those rows)."

 

You might want to review your requirements and post a clear and concise rule which observations from the concatenation of the datasets you want.

And if you do not want to concatenate the datasets, but merge observations, then we need another method altogether.

 

Please post a sample of your input datasets and the wanted dataset, as it results from these example datasets.

Your initial example has data in the "want" dataset that is not present in the "have" datasets.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 87

Re: How to Merge / Combine two tables using PROC SQL or Data Merge or Combine

Posted in reply to KurtBremser

it is returning a table with headers only. no data.

Ask a Question
Discussion stats
  • 7 replies
  • 349 views
  • 2 likes
  • 3 in conversation