How can we determine which Dataset contributed an observation?

Reply
N/A
Posts: 0

How can we determine which Dataset contributed an observation?

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
N/A
Posts: 0

Re: How can we determine which Dataset contributed an observation?

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
N/A
Posts: 0

Re: How can we determine which Dataset contributed an observation?

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
N/A
Posts: 1

Re: How can we determine which Dataset contributed an observation?

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.
N/A
Posts: 0

Re: How can we determine which Dataset contributed an observation?

Hi,

Thanks a lot for you time and patience.

Kind Regards,
Kamal.
Post a Question
Discussion Stats
  • 4 replies
  • 491 views
  • 0 likes
  • 2 in conversation