How to merge tables without common variables ?

  • Follow


Hi all,
I have the following 2 datasets.

data one;
	id = 1; var1 = 4; output;
	id = 1; var1 = 1; output;
	id = 1; var1 = 3; output;
	id = 2; var1 = 14; output;
	id = 2; var1 = 24; output;
	id = 2; var1 = 38; output;
run;
data two;
	 name = 'a';output;
	 name = 'b';output;
	 name = 'c';output;
run;

Required Output:

Id Var1 name
1  4      a
1  1      b
1   3     c
2   14   a
2  24     b
2   38   c

How can I add the column name in the output ? There is no common
variable in both the tables.
In the output the number of records for every Id will be 3 only and
the records in the Two dataset are also 3.

What are the different ways to achieve the required output ??

Thanks in Advance.

Regards,
Amar Mundankar.
0
Reply Amar 4/28/2010 5:00:32 AM

On Apr 28, 1:00=A0am, Amar Mundankar <amarmundan...@gmail.com> wrote:
> Hi all,
> I have the following 2 datasets.
>
> data one;
> =A0 =A0 =A0 =A0 id =3D 1; var1 =3D 4; output;
> =A0 =A0 =A0 =A0 id =3D 1; var1 =3D 1; output;
> =A0 =A0 =A0 =A0 id =3D 1; var1 =3D 3; output;
> =A0 =A0 =A0 =A0 id =3D 2; var1 =3D 14; output;
> =A0 =A0 =A0 =A0 id =3D 2; var1 =3D 24; output;
> =A0 =A0 =A0 =A0 id =3D 2; var1 =3D 38; output;
> run;
> data two;
> =A0 =A0 =A0 =A0 =A0name =3D 'a';output;
> =A0 =A0 =A0 =A0 =A0name =3D 'b';output;
> =A0 =A0 =A0 =A0 =A0name =3D 'c';output;
> run;
>
> Required Output:
>
> Id Var1 name
> 1 =A04 =A0 =A0 =A0a
> 1 =A01 =A0 =A0 =A0b
> 1 =A0 3 =A0 =A0 c
> 2 =A0 14 =A0 a
> 2 =A024 =A0 =A0 b
> 2 =A0 38 =A0 c
>
> How can I add the column name in the output ? There is no common
> variable in both the tables.
> In the output the number of records for every Id will be 3 only and
> the records in the Two dataset are also 3.
>
> What are the different ways to achieve the required output ??
>
> Thanks in Advance.
>
> Regards,
> Amar Mundankar.

I think you might try stacking the data instead by using the 'SET'
command:

DATA NEWFILE;
SET old1 old2;
RUN;

Cheers,
M
0
Reply Matt 4/28/2010 12:40:17 PM


On Apr 28, 12:00=A0am, Amar Mundankar <amarmundan...@gmail.com> wrote:
> Hi all,
> I have the following 2 datasets.
>
> data one;
> =A0 =A0 =A0 =A0 id =3D 1; var1 =3D 4; output;
> =A0 =A0 =A0 =A0 id =3D 1; var1 =3D 1; output;
> =A0 =A0 =A0 =A0 id =3D 1; var1 =3D 3; output;
> =A0 =A0 =A0 =A0 id =3D 2; var1 =3D 14; output;
> =A0 =A0 =A0 =A0 id =3D 2; var1 =3D 24; output;
> =A0 =A0 =A0 =A0 id =3D 2; var1 =3D 38; output;
> run;
> data two;
> =A0 =A0 =A0 =A0 =A0name =3D 'a';output;
> =A0 =A0 =A0 =A0 =A0name =3D 'b';output;
> =A0 =A0 =A0 =A0 =A0name =3D 'c';output;
> run;
>
> Required Output:
>
> Id Var1 name
> 1 =A04 =A0 =A0 =A0a
> 1 =A01 =A0 =A0 =A0b
> 1 =A0 3 =A0 =A0 c
> 2 =A0 14 =A0 a
> 2 =A024 =A0 =A0 b
> 2 =A0 38 =A0 c
>
> How can I add the column name in the output ? There is no common
> variable in both the tables.
> In the output the number of records for every Id will be 3 only and
> the records in the Two dataset are also 3.
>
> What are the different ways to achieve the required output ??
>
> Thanks in Advance.
>
> Regards,
> Amar Mundankar.

If you have less than three obs per ID in WORK.ONE this wont work
properly.

data three;
   do point=3D1 to nobs;
      set one;
      set two point=3Dpoint nobs=3Dnobs;
      output;
      end;
   run;
0
Reply data 4/28/2010 12:47:36 PM

On Apr 28, 5:47=A0pm, "data _null_;" <datan...@gmail.com> wrote:
> On Apr 28, 12:00=A0am, Amar Mundankar <amarmundan...@gmail.com> wrote:
>
>
>
>
>
> > Hi all,
> > I have the following 2 datasets.
>
> > data one;
> > =A0 =A0 =A0 =A0 id =3D 1; var1 =3D 4; output;
> > =A0 =A0 =A0 =A0 id =3D 1; var1 =3D 1; output;
> > =A0 =A0 =A0 =A0 id =3D 1; var1 =3D 3; output;
> > =A0 =A0 =A0 =A0 id =3D 2; var1 =3D 14; output;
> > =A0 =A0 =A0 =A0 id =3D 2; var1 =3D 24; output;
> > =A0 =A0 =A0 =A0 id =3D 2; var1 =3D 38; output;
> > run;
> > data two;
> > =A0 =A0 =A0 =A0 =A0name =3D 'a';output;
> > =A0 =A0 =A0 =A0 =A0name =3D 'b';output;
> > =A0 =A0 =A0 =A0 =A0name =3D 'c';output;
> > run;
>
> > Required Output:
>
> > Id Var1 name
> > 1 =A04 =A0 =A0 =A0a
> > 1 =A01 =A0 =A0 =A0b
> > 1 =A0 3 =A0 =A0 c
> > 2 =A0 14 =A0 a
> > 2 =A024 =A0 =A0 b
> > 2 =A0 38 =A0 c
>
> > How can I add the column name in the output ? There is no common
> > variable in both the tables.
> > In the output the number of records for every Id will be 3 only and
> > the records in the Two dataset are also 3.
>
> > What are the different ways to achieve the required output ??
>
> > Thanks in Advance.
>
> > Regards,
> > Amar Mundankar.
>
> If you have less than three obs per ID in WORK.ONE this wont work
> properly.
>
> data three;
> =A0 =A0do point=3D1 to nobs;
> =A0 =A0 =A0 set one;
> =A0 =A0 =A0 set two point=3Dpoint nobs=3Dnobs;
> =A0 =A0 =A0 output;
> =A0 =A0 =A0 end;
> =A0 =A0run;- Hide quoted text -
>
> - Show quoted text -

Hi ,
This is what I wanted.
Thanks you all for your solution.

Thanks and Regards,
Amar Mundankar.
0
Reply Amar 4/28/2010 2:52:52 PM

On Apr 28, 1:00=A0am, Amar Mundankar <amarmundan...@gmail.com> wrote:
> Hi all,
> I have the following 2 datasets.
>
> data one;
> =A0 =A0 =A0 =A0 id =3D 1; var1 =3D 4; output;
> =A0 =A0 =A0 =A0 id =3D 1; var1 =3D 1; output;
> =A0 =A0 =A0 =A0 id =3D 1; var1 =3D 3; output;
> =A0 =A0 =A0 =A0 id =3D 2; var1 =3D 14; output;
> =A0 =A0 =A0 =A0 id =3D 2; var1 =3D 24; output;
> =A0 =A0 =A0 =A0 id =3D 2; var1 =3D 38; output;
> run;
> data two;
> =A0 =A0 =A0 =A0 =A0name =3D 'a';output;
> =A0 =A0 =A0 =A0 =A0name =3D 'b';output;
> =A0 =A0 =A0 =A0 =A0name =3D 'c';output;
> run;
>
> Required Output:
>
> Id Var1 name
> 1 =A04 =A0 =A0 =A0a
> 1 =A01 =A0 =A0 =A0b
> 1 =A0 3 =A0 =A0 c
> 2 =A0 14 =A0 a
> 2 =A024 =A0 =A0 b
> 2 =A0 38 =A0 c
>
> How can I add the column name in the output ? There is no common
> variable in both the tables.
> In the output the number of records for every Id will be 3 only and
> the records in the Two dataset are also 3.
>
> What are the different ways to achieve the required output ??

Amar:

One way is to use a DO loop to pointedly iterate over the second table
while the implicit DATA Step loop iterates over the first table.

This sample code uses a mod function just in case the first table has
more rows in a group than there are rows in the second table.

--------
data one;
input id var1 @@;
datalines;
1  4 1  1 1  3
2 14 2 24 2 38
run;

data two;
input name $ @@;
datalines;
a b c
run;

data want;
  set one;
  by id;

  if first.id then
    index =3D 1;
  else
    index =3D mod(index,maxindex)+1;

  set two nobs=3Dmaxindex point=3Dindex;
run;
--------

A second way would involve creating synthetic keys for each table and
joining them with SQL


--
Richard A. DeVenezia
http://www.devenezia.com
0
Reply Richard 4/28/2010 3:14:52 PM

4 Replies
363 Views

(page loaded in 0.189 seconds)

Similiar Articles:













7/25/2012 11:42:46 PM


Reply: