BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ANKH1
Pyrite | Level 9

Hi! I want to merge datasets by ID. Each dataset has different ages. I would like to get a merged dataset that looks like this:

IDage1age2age3
11213 
1 23 
2 25 
311 22
3  32
3  33
410 12
415  

I used this code

 

data ages_all;
merge
age1 age2 age3;
by ID;
run;

The problem is that I get this dataset:

 

IDage1age2age3
11213 
11223 
2 25 
311 22
311 32
311 33
410 12
415 12

As you can see, for example for ID=1, it has two different observations for age2 and only one observation for age1. However, after merging, the value for age1 repeats. I would like to avoid that, since I want to calculate frequencies per variable and having two observations with the value 12 for ID is not correct.

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @ANKH1,

 

You can reset the age variables to missing by inserting

call missing(ID, of age1-age3);

before the MERGE statement (variable ID is included optionally just to maintain variable order).

 

However, I wouldn't merge the datasets this way with the intention "to calculate frequencies per variable."

 


@ANKH1 wrote:

(...) having two observations with the value 12 for ID is not correct.


Note that having an observation with missing age1 for ID 1 is actually not correct either.

 

Frequencies could be calculated from the original datasets or after interleaving them, e.g., like this:

data ages_all;
set age1(rename=(age1=age) in=in1)
    age2(rename=(age2=age) in=in2)
    age3(rename=(age3=age) in=in3);
by id;
ds=in1+2*in2+3*in3;
run;

(Instead of RENAME= and IN= dataset options you could use the COALESCE function and the INDSNAME= option of the SET statement, respectively.)

 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Sorry, I'm not understanding. You have variables AGE1 AGE2 and AGE3, then you talk about merging data sets AGE1 AGE2 and AGE3. That doesn't seem right.

 

Please show us (a portion of) all the data sets involved here. Please provide data as WORKING data step code (examples and instructions).

--
Paige Miller
ANKH1
Pyrite | Level 9
Hi! Sorry, the datasets names are different. I am aware of that. Just changed the names to publish here.
PaigeMiller
Diamond | Level 26

Please read my request in my previous message, where I said:

 

Please show us (a portion of) all the data sets involved here. Please provide data as WORKING data step code (examples and instructions).

--
Paige Miller
FreelanceReinh
Jade | Level 19

Hi @ANKH1,

 

You can reset the age variables to missing by inserting

call missing(ID, of age1-age3);

before the MERGE statement (variable ID is included optionally just to maintain variable order).

 

However, I wouldn't merge the datasets this way with the intention "to calculate frequencies per variable."

 


@ANKH1 wrote:

(...) having two observations with the value 12 for ID is not correct.


Note that having an observation with missing age1 for ID 1 is actually not correct either.

 

Frequencies could be calculated from the original datasets or after interleaving them, e.g., like this:

data ages_all;
set age1(rename=(age1=age) in=in1)
    age2(rename=(age2=age) in=in2)
    age3(rename=(age3=age) in=in3);
by id;
ds=in1+2*in2+3*in3;
run;

(Instead of RENAME= and IN= dataset options you could use the COALESCE function and the INDSNAME= option of the SET statement, respectively.)

 

ANKH1
Pyrite | Level 9

Thank you so much! The output was what I needed!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 5 replies
  • 660 views
  • 0 likes
  • 3 in conversation