<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic using multiple first. and last. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/617994#M181211</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; I am trying to get multiple transactions happened with customer accounts with travel agent to a single travel account. But, I don't want to see the same customer having the same travel account transactions (single or multiple transactions). The data looks like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date mmddyy10. cust_account_id$ travel_agent$ trasfer_amount travel_account_id$;
format date mmddyy10.;
cards;
1/15/2018 wxyz dave 1000 abcd
2/14/2017 wxyz dave 2000 abcd
8/17/1998 mnop jason 8521 defg
05/24/1999 klmn greg 8754 opqr
11/30/2000 ryan greg 12548 opqr
06/25/2011 mall greg 1487 opqr
12/25/2013 john molly 2594 matt
10/28/2019 jim molly 5000 matt
12/23/2019 jim molly 10000 matt
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want the output dataset to look like&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;05/24/1999 klmn greg 8754 opqr
11/30/2000 ryan greg 12548 opqr
06/25/2011 mall greg 1487 opqr
12/25/2013 john molly 2594 matt
10/28/2019 jim molly 5000 matt
12/23/2019 jim molly 10000 matt&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used following steps:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have ;
by travel_account_id travel_agent;
run;

data want1;
set have;
by travel_account_id ;
if first.travel_account_id and last.travel_account_id then delete;
else output want1;
run;

proc sort data=want1;
by cust_account_id;
run;

data want2;
set want1;
by cust_account_id;
if first.cust_account_id and last.cust_account_id then output;
else delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am unable to get the output i want. Could you please share your thoughts on this please??&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Jan 2020 04:22:12 GMT</pubDate>
    <dc:creator>buddha_d</dc:creator>
    <dc:date>2020-01-17T04:22:12Z</dc:date>
    <item>
      <title>using multiple first. and last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/617994#M181211</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; I am trying to get multiple transactions happened with customer accounts with travel agent to a single travel account. But, I don't want to see the same customer having the same travel account transactions (single or multiple transactions). The data looks like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date mmddyy10. cust_account_id$ travel_agent$ trasfer_amount travel_account_id$;
format date mmddyy10.;
cards;
1/15/2018 wxyz dave 1000 abcd
2/14/2017 wxyz dave 2000 abcd
8/17/1998 mnop jason 8521 defg
05/24/1999 klmn greg 8754 opqr
11/30/2000 ryan greg 12548 opqr
06/25/2011 mall greg 1487 opqr
12/25/2013 john molly 2594 matt
10/28/2019 jim molly 5000 matt
12/23/2019 jim molly 10000 matt
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want the output dataset to look like&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;05/24/1999 klmn greg 8754 opqr
11/30/2000 ryan greg 12548 opqr
06/25/2011 mall greg 1487 opqr
12/25/2013 john molly 2594 matt
10/28/2019 jim molly 5000 matt
12/23/2019 jim molly 10000 matt&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used following steps:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have ;
by travel_account_id travel_agent;
run;

data want1;
set have;
by travel_account_id ;
if first.travel_account_id and last.travel_account_id then delete;
else output want1;
run;

proc sort data=want1;
by cust_account_id;
run;

data want2;
set want1;
by cust_account_id;
if first.cust_account_id and last.cust_account_id then output;
else delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am unable to get the output i want. Could you please share your thoughts on this please??&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2020 04:22:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/617994#M181211</guid>
      <dc:creator>buddha_d</dc:creator>
      <dc:date>2020-01-17T04:22:12Z</dc:date>
    </item>
    <item>
      <title>Re: using multiple first. and last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/617998#M181212</link>
      <description>&lt;P&gt;Can you explain better what you are trying to do?&lt;/P&gt;
&lt;P&gt;Also explain what you think the code you posted is going to do and how you think that helps you get to the answer you want.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2020 04:27:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/617998#M181212</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-01-17T04:27:40Z</dc:date>
    </item>
    <item>
      <title>Re: using multiple first. and last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/617999#M181213</link>
      <description>&lt;P&gt;Here is one way to do this (if I understood your question):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have; by travel_agent cust_account_id; run;

data want;
do until(last.travel_agent);
	set have; by travel_agent cust_account_id;
	if first.cust_account_id and not first.travel_agent then keep=1;
	end;
do until(last.travel_agent);
	set have; by travel_agent cust_account_id;
	if keep then output;
	end;
drop keep;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Jan 2020 04:32:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/617999#M181213</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-01-17T04:32:53Z</dc:date>
    </item>
    <item>
      <title>Re: using multiple first. and last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/618001#M181215</link>
      <description>&lt;P&gt;Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;I am trying to get the records that have the same travel agent making multiple customer account transactions to a single travel account id. For example these below records are perfect for the above description (where multiple customer accounts (klmn, ryan and mall) transactions were done by same travel agent (greg) for the same travel account id (opqr).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;05/24/1999 klmn greg 8754 opqr&lt;BR /&gt;11/30/2000 ryan greg 12548 opqr&lt;BR /&gt;06/25/2011 mall greg 1487 opqr&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At the same time, molly (travel agent) had multiple customer account ids (john and jim)&amp;nbsp; has single travel account id (matt). But in other records (unique record meaning single customer id and travel account id) need to be deleted and at the same time&amp;nbsp; dave (travel agent) has same customer id as well as travel id (which I don't want).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, in my first step (proc sort)&amp;nbsp; I sorted and deleted unique records, but I am unable to accomplish the second criteria to delete these records in my data (below are the records)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1/15/2018 wxyz dave 1000 abcd&lt;BR /&gt;2/14/2017 wxyz dave 2000 abcd&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2020 04:38:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/618001#M181215</guid>
      <dc:creator>buddha_d</dc:creator>
      <dc:date>2020-01-17T04:38:35Z</dc:date>
    </item>
    <item>
      <title>Re: using multiple first. and last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/618002#M181216</link>
      <description>&lt;P&gt;Sounds like you want this query:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select * from have
  group by travel_agent , travel_account_id
  having  count(distinct cust_account_id) &amp;gt; 1
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you wanted to do it with data step then first sort,&amp;nbsp; for each group count how many customers and the based on number of customers output the data or not.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have ;
  by travel_agent travel_account_id cust_account_id ;
run;

data want ;
do until (last.travel_account_id);
  set have;
  by travel_agent travel_account_id cust_account_id ;
  num_cust = sum(num_cust,first.cust_account_id);
end;
do until (last.travel_account_id);
  set have;
  by travel_agent travel_account_id cust_account_id ;
  if num_cust &amp;gt; 1 then output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Jan 2020 04:47:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/618002#M181216</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-01-17T04:47:45Z</dc:date>
    </item>
    <item>
      <title>Re: using multiple first. and last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/618003#M181217</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's see if I understand this correctly&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;You want to eliminate any record that represents the only record for a given &lt;EM&gt;travel_account_id&lt;/EM&gt;.&amp;nbsp; So drop the&lt;BR /&gt;&amp;nbsp; 8/17/1998 mnop jason 8521 defg&lt;BR /&gt;record.&lt;/LI&gt;
&lt;LI&gt;But what is the rule you are using to drop these two records?&lt;BR /&gt;1/15/2018 wxyz dave 1000 abcd&amp;nbsp;&lt;BR /&gt;2/14/2017 wxyz dave 2000 abcd&lt;BR /&gt;Is it because &lt;EM&gt;travel_account_id&lt;/EM&gt; "abcd" has only one customer (2 records for &lt;EM&gt;cust_account_id&lt;/EM&gt; "wxyz")?&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Or does this have to somehow involve the &lt;EM&gt;travel_agent&lt;/EM&gt; ("dave") as well?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;If I an correct with #1 and #2,&amp;nbsp; and #3 is irrelevant (i.e. no need to consider &lt;EM&gt;travel_agent&lt;/EM&gt;), then there is only one rule: Drop all records in which a given travel_account_id has only one cust_account_id&amp;nbsp; (i.e. rule 2 includes rule 1).&amp;nbsp; If this is the case, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=need;
  by travel_account_id ;
run;
data want2 (drop=more_than_one_cust);
  set need (in=firstpass)   need (in=secondpass);
  by travel_account_id;
  retain more_than_one_cust;
  if firstpass then do;
    if cust_account_id^=lag(cust_account_id) then more_than_one_cust='Y';
    if first.travel_account_id then more_than_one_cust='N';
  end;
  if secondpass and more_than_one_cust='Y';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2020 04:48:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/618003#M181217</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-01-17T04:48:51Z</dc:date>
    </item>
    <item>
      <title>Re: using multiple first. and last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/618247#M181342</link>
      <description>&lt;P&gt;Thank you all for your replies.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2020 10:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-multiple-first-and-last/m-p/618247#M181342</guid>
      <dc:creator>buddha_d</dc:creator>
      <dc:date>2020-01-18T10:28:18Z</dc:date>
    </item>
  </channel>
</rss>

