BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ercksh8
Fluorite | Level 6

Hello all. So currently I am trying to create a table that will merge data from 4 datasets (say Y1, Y2, Y3, Y4).

The 4 datasets are panel data from year 1, 2, 3, and 4 respectively. I have merged the four data sets using the data merge by ID. When I viewed the resulting data, I realized that there were some IDs that are not present in all four datasets. I would like to be able to make a dataset that merges only the IDs that are present in all four years.

 

After doing so, I would like to create a new variable that adds all expenditures that were made by each ID in all 4 years, if they meet a separate condition. (i.e. if their zip code starts with the number 5). 

 

Y1

IDExpenditureZipcode
15020000
214030000
33051000
44052000
55050000

Y2

IDExpenditureZipcode
610050000
211030000
312051000
413052000
5140

50000

Y3

IDExpenditureZipcode
15020000
214030000
33051000
44052000
550

50000

Y4

IDExpenditureZipcode
15020000
214030000
33051000
44052000

If the datasets look something like this, the first step would create a data table with IDs 2, 3, 4 since they are the only ones present in all 4 datasets. Then the second part would create a new table with only IDs 3, 4 and would have an additional column sum_expenditure.

 

I'm sorry my question is so all over the place. Thank you all in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

data all;

merge y1(in=in1)

           y2(in=in2)

           y3(in=in3)

           y4(in=in4)

; by ID;

   if in1 and in2 and in3 and in4;

run;

View solution in original post

1 REPLY 1
Shmuel
Garnet | Level 18

data all;

merge y1(in=in1)

           y2(in=in2)

           y3(in=in3)

           y4(in=in4)

; by ID;

   if in1 and in2 and in3 and in4;

run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 679 views
  • 0 likes
  • 2 in conversation