<?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 Re: How to keep all accounts based on other variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924348#M363845</link>
    <description>&lt;P&gt;UNTESTED CODE (I can't program code to pull numbers out of a screen capture)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
    set have;
    if type=136 then flag=1; else flag=0;
run;
proc summary data=have2;
     class account_number;
     var flag;
     output out=maxx(drop=_:) max=;
run;
data want;
    merge have maxx;
    by account_number;
    if flag=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 15 Apr 2024 12:03:43 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2024-04-15T12:03:43Z</dc:date>
    <item>
      <title>How to keep all accounts based on other variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924345#M363843</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone tell me what the best way to keep all previous records if one account has type = 136 (type is a character variable)? In the example below, since account numbers 12 and 18 have one record where type=136, I want to keep all instances where the same account appears (in order to track what the previous types were). Since my dataset has over 500,000 accounts, is there code that would quickly do this ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Justin9_1-1713181448863.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95545i1A07099F344410EB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Justin9_1-1713181448863.png" alt="Justin9_1-1713181448863.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2024 11:45:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924345#M363843</guid>
      <dc:creator>Justin9</dc:creator>
      <dc:date>2024-04-15T11:45:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep all accounts based on other variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924348#M363845</link>
      <description>&lt;P&gt;UNTESTED CODE (I can't program code to pull numbers out of a screen capture)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
    set have;
    if type=136 then flag=1; else flag=0;
run;
proc summary data=have2;
     class account_number;
     var flag;
     output out=maxx(drop=_:) max=;
run;
data want;
    merge have maxx;
    by account_number;
    if flag=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2024 12:03:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924348#M363845</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-04-15T12:03:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep all accounts based on other variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924424#M363869</link>
      <description>&lt;P&gt;If your data are sorted by account_number:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have (where=(type='136') in=wanted)
        have;
  by account_number;
  if wanted=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The WANTED dummy variable will be a 1 for all account numbers in have that include at least 1 instance of "136".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's due to the way a MERGE statement followed by a BY statement works.&amp;nbsp; For each account number, whichever merge data set has fewer obs will have its last instance matched against the "excess" obs in the other data set, including the dummy varaible associated with the "shorter" data set.&amp;nbsp; The "136" obs are the shorted dataset in this case.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2024 20:57:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924424#M363869</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-04-15T20:57:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep all accounts based on other variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924427#M363872</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/321018"&gt;@Justin9&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see the code below :&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data test1 test2;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set test;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;by acct ;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if first.acct then n =1; else n+1;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if type = '136' then f=1;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;output test1 ;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if f=1 then output test2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data test3;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;merge test1(in=a) test2(in=b keep=acct n rename=(n=n_) );&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;by acct;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if a and b and&amp;nbsp; n le n_ ;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;drop n n_ f;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;</description>
      <pubDate>Mon, 15 Apr 2024 21:30:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924427#M363872</guid>
      <dc:creator>ItsMeAG</dc:creator>
      <dc:date>2024-04-15T21:30:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep all accounts based on other variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924431#M363875</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/321018"&gt;@Justin9&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is some ambiguity in your request.&amp;nbsp; You start out by saying&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;Can someone tell me what the best way to keep all previous records if one account has type = 136 (type is a character variable)?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;but later you say&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;where type=136, I want to keep all instances where the same account appears&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So which is it?&amp;nbsp;&amp;nbsp;&lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;All obs&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt; for the identified account?&amp;nbsp; Or just the &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;preceding obs&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;?&lt;/P&gt;
&lt;P&gt;And if it's the preceding obs, do you want the type=136 obs also?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My solution, and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s provide all the obs.&amp;nbsp; But&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446777"&gt;@ItsMeAG&lt;/a&gt;'s response selects every obs through the last type="136".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, it would simplify code to know:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Can your data have more than one type="136" for a single account?&lt;/LI&gt;
&lt;LI&gt;Is your data sorted by type, within account?&amp;nbsp; You sample data is.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Mon, 15 Apr 2024 22:01:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924431#M363875</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-04-15T22:01:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep all accounts based on other variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924432#M363876</link>
      <description>&lt;P&gt;After seeing&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446777"&gt;@ItsMeAG&lt;/a&gt;&amp;nbsp;'s response, it occurred to me that you want only obs up through the last type='136', but not any obs that follow.&amp;nbsp; If so, here is a possibility:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have (where=(type='136') in=found136)
      have (in=secondpass);
  by account_number;

  if first.account_number=1 then _n136=0;
  _n136+found136;

  if secondpass and _n136&amp;gt;0;    *subsetting IF*; 
  if type='136' then _n136=_n136-1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2024 22:06:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924432#M363876</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-04-15T22:06:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep all accounts based on other variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924445#M363882</link>
      <description>proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select * from have &lt;BR /&gt; where account_number in&lt;BR /&gt;  (select distinct account_number from have where type='136')&lt;BR /&gt;;&lt;BR /&gt;quit;</description>
      <pubDate>Tue, 16 Apr 2024 02:27:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-all-accounts-based-on-other-variable/m-p/924445#M363882</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-04-16T02:27:08Z</dc:date>
    </item>
  </channel>
</rss>

