BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

Can any one pls let me know how to know which dataset contributed an observation? Pls gimme an example with code.

Thanks in advance
Kamal
4 REPLIES 4
deleted_user
Not applicable
Hi Kamal,

I hope you have this query in the context of a merge.

When a SET or MERGE statement reads multiple SAS datasets, you can use IN=datatset option to keep track of the source of observations. This IN=dataset is useful only for input SAS datasets in a data step. This variable is a Boolean variable with a value of 1 if the observation contains data from that SAS dataset, 0 if it does not. You should use use dift. variable name for each datatset.

Consider this MERGE statement:

MERGE A (IN=INA) B (IN=INB);
BY X;

The variable INA and INB tell you whether each BY group is found in the two input SAS datasets. If both SAS datatsets contain the BY group, then both INA and INB have values of 1. If only A contains the BY group, then INA is 1 and INB is 0. Or if only B contains the BY group, then INA is 0 and INB is 1.

In a merge, you might want to keep observations only if the key value is present in a specific SAS datatset. Use a subsetting IF statement to select these observations. The eg. below keeps only observations that are found in A. It discards observations that are found in B.

MERGE A (IN=INA) B;
BY X;
IF INA;

note: IF INA is same as saying IF INA=1;

Hope this helps.

Thanks

Cathy
deleted_user
Not applicable
Hi Cathy,

Thank you for your time. Is there any difference between 'Observations from only INA' and 'Observations from only INA but not INB'. Then how to write code in that scenario?

Kind Regards,
Kamal.
G
Calcite | Level 5 G
Calcite | Level 5
Hi,

The automatic IN variable is a retained variable so that if dataset A has two records
and dataset B has one record for a by group then the non-by variable values in dataset B and the dataset B IN variable are retained. To see if a dataset is contributing a new physical record, not a retained record do the following.

data x;

ina=0;
inb=0;

merge a(in=ina) b(in=inb);
by x;


By setting the retain IN varibale to 0 before the merge statment, it forces SAS to reset it to 1 if a new physical record is read, otherwise it will be 0.

You can have this functionality and the default at the same time by doing the following:

data x;
retain inaX inbX 0;
ina=0;
inb=0;

merge a(in=ina) b(in=inb);
by x;
if first.x then
do;
inaX=ina;
inabX=inb;
end;

This way inaX and inbX behave with the default functionality and you have the new functionality of ina and inb.
deleted_user
Not applicable
Hi,

Thanks a lot for you time and patience.

Kind Regards,
Kamal.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Health and Life Sciences Learning

 

Need courses to help you with SAS Life Sciences Analytics Framework, SAS Health Cohort Builder, or other topics? Check out the Health and Life Sciences learning path for all of the offerings.

LEARN MORE

Discussion stats
  • 4 replies
  • 3318 views
  • 1 like
  • 2 in conversation