BookmarkSubscribeRSS Feed
ranikeka
Calcite | Level 5
Hi

I have sheet 1 and sheet 2 in excel file.
I want sheet 1 main info to corresponding observations in sheet 2.
I have created datasets in sas and need to combine. So I have used merge but the main info isn’t have corresponding values in sheet2?

For example:
Sheet 1 - one observation corresponds to 40 etc.. observations in sheet 2. And so on
Like this I have numerous excel files which I combined into single dataset.
Could you please help with me this.

Thanks


7 REPLIES 7
Kurt_Bremser
Super User

Please give us an example for the two datasets after import into SAS. Post the datasets as data steps with datalines, and post the code into a window opened with the "little running man" button.

cass5957
Calcite | Level 5

Hi there!

You might have some luck using "MERGE" in the DATA step.

DATA WORK.DataSet;

MERGE Set.Sheet1

              Set.Sheet2;

RUN;

You also might need to sort the data before merging to make sure the variables so everything matches up. 

Hope this helps!

ranikeka
Calcite | Level 5

HI

 

Thanks for the suggestion. I dont have same variable names thats the problem. Here is my sample file to understand better. Any help appreciated.

sheet 1

abc defghi
1aa1
2ba2
3ca3
4da4

sheet 2

abc defghijklmnopqr
1aa1xxxxrrrrtttt
1aa1vvvvvhhhhtttt
1aa1nnnnnggggg
1aa1sssssbbbjj
1aa1eeeeeccckk
2ba2tttthhhhnnn
2ba2tttthhhhnnn
2ba2tttthhhhnnn
3ca3fffffttttuuuu
3ca3ppppqqqqeeee
3ca3ttttgggtttt
3ca3lllpppiii

 

Kurt_Bremser
Super User

See this:

data sheet1;
input abc $ def $ ghi $;
datalines;
1  a a1
2  b a2
3  c a3
4  d a4
;

data sheet2;
input abc $ def $ ghi $ jkl $ mno $ pqr $;
datalines;
1  a a1  xxxx  rrrr  tttt
1  a a1  vvvvv hhhh  tttt
1  a a1  nnnnn ggg gg
1  a a1  sssss bbb jj
1  a a1  eeeee ccc kk
2  b a2  tttt  hhhh  nnn
2  b a2  tttt  hhhh  nnn
2  b a2  tttt  hhhh  nnn
3  c a3  fffff tttt  uuuu
3  c a3  pppp  qqqq  eeee
3  c a3  tttt  ggg tttt
3  c a3  lll ppp iii
;

data want;
merge
  sheet1
  sheet2
;
by abc def ghi;
run;

proc print data=want noobs;
run;

Result:

abc	def	ghi	jkl	mno	pqr
1	a	a1	xxxx	rrrr	tttt
1	a	a1	vvvvv	hhhh	tttt
1	a	a1	nnnnn	ggg	gg
1	a	a1	sssss	bbb	jj
1	a	a1	eeeee	ccc	kk
2	b	a2	tttt	hhhh	nnn
2	b	a2	tttt	hhhh	nnn
2	b	a2	tttt	hhhh	nnn
3	c	a3	fffff	tttt	uuuu
3	c	a3	pppp	qqqq	eeee
3	c	a3	tttt	ggg	tttt
3	c	a3	lll	ppp	iii
4	d	a4	 	 	 

If that does not meet your expectations, please describe in detail where it differs.

ranikeka
Calcite | Level 5

Many Thanks

 

can i use same code for below one as well

 

sheet1

abc defghi
1aa1
2ba2
3ca3
4da4

sheet 2

jklmnopqr
xxxxrrrrtttt
vvvvvhhhhtttt
nnnnnggggg
sssssbbbjj
eeeeeccckk
tttthhhhnnn
tttthhhhnnn
tttthhhhnnn
fffffttttuuuu
ppppqqqqeeee
ttttgggtttt
lllpppiii

 

I want to combine two above sheets like following

abc defghijklmnopqr
1aa1xxxxrrrrtttt
1aa1vvvvvhhhhtttt
1aa1nnnnnggggg
1aa1sssssbbbjj
1aa1eeeeeccckk
2ba2tttthhhhnnn
2ba2tttthhhhnnn
2ba2tttthhhhnnn
3ca3fffffttttuuuu
3ca3ppppqqqqeeee
3ca3ttttgggtttt
3ca3lllpppiii
Kurt_Bremser
Super User

No, unless you can define a clear rule for joining that uses the content in the available variables, or other values, like observation numbers (obs 1 from sheet1 join to obs 1-4 of sheet2, obs 2 from sheet1 to obs 5-8 from sheet2, and so on).

 

mkeintz
PROC Star

@ranikeka wrote:

Many Thanks

 

can i use same code for below one as well

 

sheet1

abc  def ghi
1 a a1
2 b a2
3 c a3
4 d a4

sheet 2

jkl mno pqr
xxxx rrrr tttt
vvvvv hhhh tttt
nnnnn ggg gg
sssss bbb jj
eeeee ccc kk
tttt hhhh nnn
tttt hhhh nnn
tttt hhhh nnn
fffff tttt uuuu
pppp qqqq eeee
tttt ggg tttt
lll ppp iii

 

I want to combine two above sheets like following

abc  def ghi jkl mno pqr
1 a a1 xxxx rrrr tttt
1 a a1 vvvvv hhhh tttt
1 a a1 nnnnn ggg gg
1 a a1 sssss bbb jj
1 a a1 eeeee ccc kk
2 b a2 tttt hhhh nnn
2 b a2 tttt hhhh nnn
2 b a2 tttt hhhh nnn
3 c a3 fffff tttt uuuu
3 c a3 pppp qqqq eeee
3 c a3 tttt ggg tttt
3 c a3 lll ppp iii

In the absence of an apparent matching rule, it looks like you are matching 1 record in sheet1 to a batch of 4 records in sheet2, in order encountered.  If that's true, you can do the below.

 

But just remember - one misplaced or deleted or duplicated record in either sheet will generate unwanted results, and there will be no obvious way to recognize that outcome.

data sheet1;
input abc $ def $ ghi $;
datalines;
1  a a1
2  b a2
3  c a3
4  d a4
;

data sheet2;
input jkl $ mno $ pqr $;
datalines;
xxxx	rrrr	tttt
vvvvv	hhhh	tttt
nnnnn	ggg	gg
sssss	bbb	jj
eeeee	ccc	kk
tttt	hhhh	nnn
tttt	hhhh	nnn
tttt	hhhh	nnn
fffff	tttt	uuuu
pppp	qqqq	eeee
tttt	ggg	tttt
lll	ppp	iii
run;

data want;
  set sheet1;
  do i=1 to 4; set sheet2; output; end;
run;

 

Note the log of this (below) reports reading 4 observations from sheet1, but that 4th obs (abc=4) is not on the output dataset.  That's because, with the 4th sheet1 record in hand, the program attempts to read the 13th from sheet2.  But there is no 13th record in sheet2, so the data step stops, with no further processing - i.e. no outputting of the record.

NOTE: There were 4 observations read from the data set WORK.SHEET1.
NOTE: There were 12 observations read from the data set WORK.SHEET2.
NOTE: The data set WORK.WANT has 12 observations and 7 variables.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1110 views
  • 0 likes
  • 4 in conversation