SAS Programming

DATA Step, Macro, Functions and more
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.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
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-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 804 views
  • 1 like
  • 3 in conversation