BookmarkSubscribeRSS Feed
dhrums28
Calcite | Level 5

Hi,

Data A:

 

sub        model 

1            CT

1             US

1             xR

1             CP

 

data B:

 

sub          model_site

1              CT

1               FG

1               YG

 

 

need final result:

 

sub        model       model_site

1            CT             CT

1            US             FG

1            xR              YG

1            CP

 

 

 

basically i have 2 different data set with subject and model. 'A' data set has 4 observations and 'B' data set has 3 observation. i need to merge this data set by Subject  but i need to last row of  model_site is missing.

 

 

 

 

 

3 REPLIES 3
Astounding
PROC Star

Assuming you have already sorted your data sets, here is the simplest way:

 

data want;

merge A B;

by sub;

output;

model = ' ';

model_site = ' ';

run;

Reeza
Super User

Do you have multiple subjects? 

 

Otherwise a MERGE without a BY statement is all you need, but I highly suspect your problem is more complicated. 

Make sure your sample data accurately reflects your real data.

Tom
Super User Tom
Super User

Normally that is not what people want to do when they have two datasets with repeating by groups, but luckily for you it is very similar to how SAS data step merge handles combining observations when datasets contribute multiple observations per group.  But normal SAS behavior is to retain the values from the last observation of the dataset with fewer observations.  

 

You cannot prevent the retaining, but you can add code to write the observation and then clear all of the variables so nothing is retained.

data want ;
  merge A B ;
  by sub ;
  output;
  call missing(of _all_);
run;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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