BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hello_there
Lapis Lazuli | Level 10

Hi, 

 

I merged a data set with another data set by subject. The one data set includes the variables arm, and armn, but the other one just includes subjects. I would like to have it so that the newly merged data set contains the same values for arm and armn after the merge for each subject. 

 

edit: in case there is confusion, the data set that is presented below is the merged data set. I'm trying to get repeat values of arm and armn for each subject so that the final data set looks like:

 

001, a, 1

001, a, 1

002, b, 2

002, b, 2

002, b, 2

003, c, 3

003, c, 3

003, c, 3

Thanks!

data have1;
infile datalines dsd dlm=",";
	input subject $ arm $ armn;
datalines;
001, a, 1
001, , ,
002, b, 2
002, b, 2
002, , 
003, c, 3
003, , 
003, , 
;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Hello_there wrote:
Hi Quentin, the data set that is presented is the new data set after the merge. I would like to know how I can repeat the values for arm and armn for each subject bc after the merged happened, the data set that did not include those variables were displayed as missing.

Then most likely you made the mistake of have the variables on BOTH of the input datasets.  The variables you want replicated should only appear on the dataset that has just the one observation per BY group.

 

So you have your subject level dataset with ARM and 

data subjects;
  input subject $ arm $ armn;
datalines;
001 a 1
002 b 2
003 c 3
;

And your dataset that has repeated measure per subject.  So perhaps heart rate measure at different visits.

data visits;
  input subject $ visit hr ;
datalines;
001 1 65
001 2 70
002 1 80
002 2 75 
003 1 72
;

Then you can merge by SUBJECT and the values of ARM and ARMN will be retained onto every observation (that is in the SUBJECTS dataset).

data want;
  merge subjects visits;
  by subject;
run;

Results

Obs    subject    arm    armn    visit    hr

 1       001       a       1       1      65
 2       001       a       1       2      70
 3       002       b       2       1      80
 4       002       b       2       2      75
 5       003       c       3       1      72


 

View solution in original post

4 REPLIES 4
Quentin
Super User

I'm confused. You're asking about merging, but you only show one dataset.  Suggest you show a small example of both datasets you are merging, and the code you used to merge them.  Then you can also show the output you get from merging them, and describe why you are unhappy with that output.

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Hello_there
Lapis Lazuli | Level 10
Hi Quentin, the data set that is presented is the new data set after the merge. I would like to know how I can repeat the values for arm and armn for each subject bc after the merged happened, the data set that did not include those variables were displayed as missing.
Tom
Super User Tom
Super User

@Hello_there wrote:
Hi Quentin, the data set that is presented is the new data set after the merge. I would like to know how I can repeat the values for arm and armn for each subject bc after the merged happened, the data set that did not include those variables were displayed as missing.

Then most likely you made the mistake of have the variables on BOTH of the input datasets.  The variables you want replicated should only appear on the dataset that has just the one observation per BY group.

 

So you have your subject level dataset with ARM and 

data subjects;
  input subject $ arm $ armn;
datalines;
001 a 1
002 b 2
003 c 3
;

And your dataset that has repeated measure per subject.  So perhaps heart rate measure at different visits.

data visits;
  input subject $ visit hr ;
datalines;
001 1 65
001 2 70
002 1 80
002 2 75 
003 1 72
;

Then you can merge by SUBJECT and the values of ARM and ARMN will be retained onto every observation (that is in the SUBJECTS dataset).

data want;
  merge subjects visits;
  by subject;
run;

Results

Obs    subject    arm    armn    visit    hr

 1       001       a       1       1      65
 2       001       a       1       2      70
 3       002       b       2       1      80
 4       002       b       2       2      75
 5       003       c       3       1      72


 

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
  • 4 replies
  • 313 views
  • 1 like
  • 3 in conversation