BookmarkSubscribeRSS Feed
kajal_30
Quartz | Level 8

ok so I have 2 datasets and want dataset as below. Can you please help how to create want dataset using 1 and 2 .

dataset1
IDname
1peter
2john
3muse
4peena
5tina

 

dataset 2
IDValue
1cbz1
2cbz2
2cbz3
2cbz4
3cbz5
3cbz6
3cbz7
4cbz8
4cbz9
4cbz10
4cbz11
5cbz12

 

want
IDnamevalue1value2value3value4
1petercbz1...
2johncbz2cbz3cbz4.
3musecbz5cbz6cbz7.
4peenacbz8cbz9cbz10cbz11
5tinacbz12...

 

 

 

 

Regards

Kajal

 

4 REPLIES 4
data_null__
Jade | Level 19

Many ways but transposing TWO has the advantage of creating as many VALUE variables as needed, you don't have to count them.

 

data one;
   infile cards expandtabs;
   input id name $;
   cards;
1	peter
2	john
3	muse
4	peena
5	tina
;;;;
   run; 

data two;
   infile cards expandtabs;
   input id value $ ;
   cards;
1	cbz1
2	cbz2
2	cbz3
2	cbz4
3	cbz5
3	cbz6
3	cbz7
4	cbz8
4	cbz9
4	cbz10
4	cbz11
5	cbz12
;;;;
   run;
   
proc transpose data=two out=wide(drop=_:) prefix=Value;
   by id;
   var value;
   run;
   
data want;
   merge one wide;
   by id;
   run;

proc print;
   run;

Capture.PNG

Tom
Super User Tom
Super User

The easiest thing is to just do a normal MERGE and leave the result alone. 

 

First let's convert your listings into actual dataset. (Hint you could have shared the data this way to begin with to make it easier for others to help you.)

data dataset1;
  input ID name $;
cards;
1  peter
2  john
3  muse
4  peena
5  tina
;

data dataset2;
  input ID Value $;
cards;
1  cbz1
2  cbz2
2  cbz3
2  cbz4
3  cbz5
3  cbz6
3  cbz7
4  cbz8
4  cbz9
4  cbz10
4  cbz11
5  cbz12
;

Now just merge the data.  You might want to add a counter variable to number the observations within each value of ID.

data want;
  merge dataset1 dataset2;
  by id;
  if first.id then row=1;
  else row+1;
run;

If you want that final listing output then use PROC REPORT with an across variable.

proc report data=want;
  column id name value,row dummy;
  define id / group;
  define name / group;
  define row / across ' ';
  define dummy / computed noprint;
  compute dummy; dummy=0; endcomp;
run;

Result

Tom_0-1719423088073.png

If you did need the data stored into multiple different variables in that wide format then just use PROC TRANSPOSE.

proc transpose data=want out=wide(drop=_name_) prefix=Value;
  by id name;
  id row;
  var value;
run;

Result

Tom_1-1719423210957.png

 

kajal_30
Quartz | Level 8

Thank you actually I overestimated myself. I thought I would be able to handle it but couldn't .

Actually there are more than 1 column along with name so not sure how to handle multiple colums.

Eg:

dataset 1 has 10 columns and dataset 2 has 12 columns.

 

Not sure how to include rest of the columns in the same code you provided.

 

Regards

Kajal

ballardw
Super User

@kajal_30 wrote:

Thank you actually I overestimated myself. I thought I would be able to handle it but couldn't .

Actually there are more than 1 column along with name so not sure how to handle multiple colums.

Eg:

dataset 1 has 10 columns and dataset 2 has 12 columns.

 

Not sure how to include rest of the columns in the same code you provided.

 

Regards

Kajal


What you can do with one variable you can do with other variables BUT naming will be different. So Transpose each of the additional variables in dataset2 into its own data set and merge all of them on the name value if you think you really need a data set.

 

The Report approach @Tom provided would with with multiple variables.

 

data dataset1;
  input ID name $;
cards;
1  peter
2  john
3  muse
4  peena
5  tina
;

data dataset2;
  input ID Value $  x $;
cards;
1  cbz1   abc
2  cbz2   pdq
2  cbz3   xyz
2  cbz4  abc
3  cbz5  pdq
3  cbz6  xyz
3  cbz7  abc
4  cbz8  pdq
4  cbz9  xyz
4  cbz10  abc
4  cbz11  pdq
5  cbz12  xyz
;

data want;
  merge dataset1 dataset2;
  by id;
  if first.id then row=1;
  else row+1;
run;

proc report data=want;
  column id name value,row x,row dummy;
  define id / group;
  define name / group;
  define row / across ' ';

  define dummy / computed noprint;
  compute dummy; dummy=0; endcomp;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 696 views
  • 4 likes
  • 4 in conversation