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.

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!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

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