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

Hi all,

 

I have some repeated longitudinal data and I want to merge them by ID. I'm trying to add the additional data as extra rows for that ID

 

Data1

ID

Date

Age

12345

01DEC2020

25

12345

02MAR2021

26

12345

03APR2022

27


Data2

ID

Date

Age

Data_Out

XYZ_Out

12345

01DEC2027

32

YES

0.21

12345

02MAR2028

33

NO

0.99

12345

03APR2029

34

YES

0.64

12345

04AUG2030

35

.

0.85

12345

05DEC2031

36

YES

0.19

 

What I'd like to create is the table below where rows from Data2 is added on top of Data1 (so that they're in chronological order based on date and age):

 

ID

Date

Age

Data_Out

XYZ_Out

12345

01DEC2020

25

 

 

12345

02MAR2021

26

 

 

12345

03APR2022

27

 

 

12345

01DEC2027

32

YES

0.21

12345

02MAR2028

33

NO

0.99

12345

03APR2029

34

YES

0.64

12345

04AUG2030

35

.

.

12345

05DEC2031

36

YES

0.19

 

The code I have right now is not merging the way I'd like as it ends up merging horizontally and duplicates the row data. It also gives me a "WARNING: Multiple lengths were specified for the BY variable Subject by input data sets" error. 

data want;

merge data1 (in=A) data2 (in=B);

by ID;

if A;

run;

Any help with this would be appreciated, thanks.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

data want;

   set data1

        data2

   ;

run;

 

And what was in your existing Have data set????? Interaction between more than one SET and/or MERGE statement is not a beginner exercise.

 

You are appending (stacking) data. SAS MERGE if for sidewise combinations to align additional (usually) variables on the same row with common key values. Though if none of the key values match between the two sets Merge will look the same.

 

Any time you combine multiple data sets variables of the same name should be made to have the same type (all numeric or all character for common names) or the process will fail. If they have different lengths you will get a message like that and may get data truncated depending on orders of things. (Can't fit 10 letters into a space that is expecting 5).

 

And usually easier to sort afterwards.

View solution in original post

1 REPLY 1
ballardw
Super User

data want;

   set data1

        data2

   ;

run;

 

And what was in your existing Have data set????? Interaction between more than one SET and/or MERGE statement is not a beginner exercise.

 

You are appending (stacking) data. SAS MERGE if for sidewise combinations to align additional (usually) variables on the same row with common key values. Though if none of the key values match between the two sets Merge will look the same.

 

Any time you combine multiple data sets variables of the same name should be made to have the same type (all numeric or all character for common names) or the process will fail. If they have different lengths you will get a message like that and may get data truncated depending on orders of things. (Can't fit 10 letters into a space that is expecting 5).

 

And usually easier to sort afterwards.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 796 views
  • 1 like
  • 2 in conversation