Help using Base SAS procedures

merge and sort

Reply
Contributor
Posts: 23

merge and sort


I want to merge temp1 and temp2  as temp4.

tem.sas is the code. It doesn't work. Basically, it cannot sort when firm=.

Please help me a little.

thanks

Attachment
Attachment
Attachment
Attachment
Super User
Posts: 18,568

Re: merge and sort

The code is correct. What isn't working as you'd expect.

You sort and merge by date not firm, so where does firm come into play?

Super Contributor
Posts: 543

Re: merge and sort

Hi.

You can't get the desired data set (temp4) because there is no firm information for data = 5. Please see below:

data temp1;

input date    firm;

cards;

1    1

2    1

3    1

4    1

1    2

2    2

3    2

;

data temp2;

input date    stock $;

cards;

1    a

2    b

3    c

4    d

5    e

;

data want;

input date    firm     stock $;

cards;

1    1    a

2    1    b

3    1    c

4    1    d

5    1    .

1    2    a

2    2    b

3    2    c

;

It seems to me that given your data set temp1 - the one that has the information on date and firm...there is no way of knowing that for date 5 you will have firm = 1 (like you want in your desired data set (temp4))

Unless you specifically tell us that for any missing information on firm carry down the information form prior record....??


So, below is the code you provide (which is correct):

proc sort data=temp1;

by date;

run;

proc sort data=temp2;

by date;

run;

data temp3;

merge temp1(in=frodo) temp2(in=prodo);

by date;

if prodo;

run;

This works, but without you specifically telling us that you want a carry down of value when missing, you can't get the desired output.


I am talking a bit circular, I think.


I hope this helps a bit.


Good luck,

Anca.

Ask a Question
Discussion stats
  • 2 replies
  • 208 views
  • 0 likes
  • 3 in conversation