<?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 Finding an observation in a sas in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/571687#M161292</link>
    <description>I need to consider first occurrence of 7400 in a group and the last occurrence of 7470/4320. I have altered the code as below&lt;BR /&gt;&lt;BR /&gt;Data want;&lt;BR /&gt;set have;&lt;BR /&gt;by Group transaction notsorted;&lt;BR /&gt;where transaction in (7400,7470,4320);&lt;BR /&gt;&lt;BR /&gt;if (last.transaction and transaction=7400 and not last.group) or (first.transaction and transaction=7470 or transaction = 4320 and not first.group);&lt;BR /&gt;&lt;BR /&gt;The code is working in for case A,B,C, E but not D&lt;BR /&gt;&lt;BR /&gt;Input&lt;BR /&gt;Group sequence transaction&lt;BR /&gt;A 100 1200 jan10&lt;BR /&gt;A 101 7400 jan10&lt;BR /&gt;A. 102 1000 jan10&lt;BR /&gt;A 103 7470 jan10&lt;BR /&gt;B 201 7400 jan10&lt;BR /&gt;B 202 4044 jan10&lt;BR /&gt;B. 203. 4600 jan10&lt;BR /&gt;B. 204. 7470 jan10&lt;BR /&gt;B. 205. 7400 jan10&lt;BR /&gt;B. 206. 7470 jan10&lt;BR /&gt;C. 301. 7400 jan10&lt;BR /&gt;C. 302. 1000 jan10&lt;BR /&gt;C. 303. 7400 jan10&lt;BR /&gt;C. 304. 7470 jan10&lt;BR /&gt;D 500 4060 jan10&lt;BR /&gt;D 401 7400 jan10&lt;BR /&gt;D 403 4320 jan10&lt;BR /&gt;D 404 7400 jan10&lt;BR /&gt;D 405 7415 jan10&lt;BR /&gt;D 406 7420 jan10&lt;BR /&gt;D 407 7400 jan10&lt;BR /&gt;D 408 7470 jan10&lt;BR /&gt;E 500 7400 jan10&lt;BR /&gt;E 501 1000 jan10&lt;BR /&gt;E 502 4320 jan10&lt;BR /&gt;E 503 7400 jan11&lt;BR /&gt;E 504 7470 jan11&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Output should look like&lt;BR /&gt;Group sequence transaction&lt;BR /&gt;A. 101. 7400 jan10&lt;BR /&gt;A. 103. 7470 jan10&lt;BR /&gt;B. 201. 7400 jan10&lt;BR /&gt;B. 204. 7470 jan10&lt;BR /&gt;B. 205. 7400 jan10&lt;BR /&gt;B. 206. 7470 jan10&lt;BR /&gt;C. 303. 7400 jan10&lt;BR /&gt;C. 304. 7470 jan10&lt;BR /&gt;D 401 7400 jan10&lt;BR /&gt;D 408 7470 jan10&lt;BR /&gt;E 500 7400 jan10&lt;BR /&gt;E 502 4320 jan10&lt;BR /&gt;E 503 7400 jan11&lt;BR /&gt;E 504 7470 jan11&lt;BR /&gt;</description>
    <pubDate>Mon, 08 Jul 2019 03:03:55 GMT</pubDate>
    <dc:creator>Divya8</dc:creator>
    <dc:date>2019-07-08T03:03:55Z</dc:date>
    <item>
      <title>Finding an observation in a sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/571687#M161292</link>
      <description>I need to consider first occurrence of 7400 in a group and the last occurrence of 7470/4320. I have altered the code as below&lt;BR /&gt;&lt;BR /&gt;Data want;&lt;BR /&gt;set have;&lt;BR /&gt;by Group transaction notsorted;&lt;BR /&gt;where transaction in (7400,7470,4320);&lt;BR /&gt;&lt;BR /&gt;if (last.transaction and transaction=7400 and not last.group) or (first.transaction and transaction=7470 or transaction = 4320 and not first.group);&lt;BR /&gt;&lt;BR /&gt;The code is working in for case A,B,C, E but not D&lt;BR /&gt;&lt;BR /&gt;Input&lt;BR /&gt;Group sequence transaction&lt;BR /&gt;A 100 1200 jan10&lt;BR /&gt;A 101 7400 jan10&lt;BR /&gt;A. 102 1000 jan10&lt;BR /&gt;A 103 7470 jan10&lt;BR /&gt;B 201 7400 jan10&lt;BR /&gt;B 202 4044 jan10&lt;BR /&gt;B. 203. 4600 jan10&lt;BR /&gt;B. 204. 7470 jan10&lt;BR /&gt;B. 205. 7400 jan10&lt;BR /&gt;B. 206. 7470 jan10&lt;BR /&gt;C. 301. 7400 jan10&lt;BR /&gt;C. 302. 1000 jan10&lt;BR /&gt;C. 303. 7400 jan10&lt;BR /&gt;C. 304. 7470 jan10&lt;BR /&gt;D 500 4060 jan10&lt;BR /&gt;D 401 7400 jan10&lt;BR /&gt;D 403 4320 jan10&lt;BR /&gt;D 404 7400 jan10&lt;BR /&gt;D 405 7415 jan10&lt;BR /&gt;D 406 7420 jan10&lt;BR /&gt;D 407 7400 jan10&lt;BR /&gt;D 408 7470 jan10&lt;BR /&gt;E 500 7400 jan10&lt;BR /&gt;E 501 1000 jan10&lt;BR /&gt;E 502 4320 jan10&lt;BR /&gt;E 503 7400 jan11&lt;BR /&gt;E 504 7470 jan11&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Output should look like&lt;BR /&gt;Group sequence transaction&lt;BR /&gt;A. 101. 7400 jan10&lt;BR /&gt;A. 103. 7470 jan10&lt;BR /&gt;B. 201. 7400 jan10&lt;BR /&gt;B. 204. 7470 jan10&lt;BR /&gt;B. 205. 7400 jan10&lt;BR /&gt;B. 206. 7470 jan10&lt;BR /&gt;C. 303. 7400 jan10&lt;BR /&gt;C. 304. 7470 jan10&lt;BR /&gt;D 401 7400 jan10&lt;BR /&gt;D 408 7470 jan10&lt;BR /&gt;E 500 7400 jan10&lt;BR /&gt;E 502 4320 jan10&lt;BR /&gt;E 503 7400 jan11&lt;BR /&gt;E 504 7470 jan11&lt;BR /&gt;</description>
      <pubDate>Mon, 08 Jul 2019 03:03:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/571687#M161292</guid>
      <dc:creator>Divya8</dc:creator>
      <dc:date>2019-07-08T03:03:55Z</dc:date>
    </item>
    <item>
      <title>Re: Finding an observation in a sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/571692#M161294</link>
      <description>&lt;P&gt;1. From now on, please post you data as a data step (and &lt;STRONG&gt;run the pasted code&lt;/STRONG&gt; to ensure it is valid) like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
 input GROUP $ SEQUENCE $ TRANSACTION DT $ ;
cards;
A 100 1200 jan10
A 101 7400 jan10
A 102 1000 jan10
A 103 7470 jan10
B 201 7400 jan10
B 202 4044 jan10
B 203 4600 jan10
B 204 7470 jan10
B 205 7400 jan10
B 206 7470 jan10
C 301 7400 jan10
C 302 1000 jan10
C 303 7400 jan10
C 304 7470 jan10
D 500 4060 jan10
D 401 7400 jan10
D 403 4320 jan10
D 404 7400 jan10
D 405 7415 jan10
D 406 7420 jan10
D 407 7400 jan10
D 408 7470 jan10
E 500 7400 jan10
E 501 1000 jan10
E 502 4320 jan10
E 503 7400 jan11
E 504 7470 jan11
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2. Your Boolean clause in the IF test is unclear because of the OR operator. Do you mean this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  set HAVE;
  by GROUP TRANSACTION notsorted;
  where TRANSACTION in (7400,7470,4320);
  if (last.TRANSACTION and TRANSACTION=7400 and not last.GROUP) 
  or (first.TRANSACTION and TRANSACTION in(7470,4320) and not first.GROUP);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;3. The test validates the values&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;D 407 7400 jan10
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which you state you do not want.&lt;/P&gt;
&lt;P&gt;Why should these values be rejected? In other words, what is the condition to reject these values?&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jul 2019 04:51:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/571692#M161294</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-07-08T04:51:00Z</dc:date>
    </item>
    <item>
      <title>Re: Finding an observation in a sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/571694#M161296</link>
      <description>2. The requirement is to get the first occurrence of 7400 transaction in a group and the last occurrence of the 7479/4320 on the same date.&lt;BR /&gt;&lt;BR /&gt;3. As per the above requirement I need to consider only the below values for D group.&lt;BR /&gt;&lt;BR /&gt;D 401 7400 jan10&lt;BR /&gt;D 408 7470 jan10&lt;BR /&gt;&lt;BR /&gt;D 500 4060 jan10&lt;BR /&gt;D 401 7400 jan10&lt;BR /&gt;D 403 4320 jan10&lt;BR /&gt;D 404 7400 jan10&lt;BR /&gt;D 405 7415 jan10&lt;BR /&gt;D 406 7420 jan10&lt;BR /&gt;D 407 7400 jan10&lt;BR /&gt;D 408 7470 jan10</description>
      <pubDate>Mon, 08 Jul 2019 05:07:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/571694#M161296</guid>
      <dc:creator>Divya8</dc:creator>
      <dc:date>2019-07-08T05:07:43Z</dc:date>
    </item>
    <item>
      <title>Re: Finding an observation in a sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/571695#M161297</link>
      <description>I have restrictions to copy paste the code. It’s was a typo error. Yes I mean the same logic that you have mentioned in the point 2</description>
      <pubDate>Mon, 08 Jul 2019 05:10:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/571695#M161297</guid>
      <dc:creator>Divya8</dc:creator>
      <dc:date>2019-07-08T05:10:21Z</dc:date>
    </item>
    <item>
      <title>Re: Finding an observation in a sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/571708#M161304</link>
      <description>&lt;P&gt;Your expected output does not match your logic. Per your logic, you can only have a maximum of two observations per group.&lt;/P&gt;
&lt;P&gt;Code according to your logic:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input group :$1. sequence transaction;
datalines;
A 100 1200
A 101 7400
A 102 1000
A 103 7470
B 201 7400
B 202 4044
B 203 4600
B 204 7470
B 205 7400
B 206 7470
C 301 7400
C 302 1000
C 303 7400
C 304 7470
D 500 4060
D 401 7400
D 403 4320
D 404 7400
D 405 7415
D 406 7420
D 407 7400
D 408 7470
E 500 7400
E 501 1000
E 502 4320
E 503 7400
E 504 7470
;

data want;
firstflag = 0;
lastcount = 0;
counter = 0;
do until (last.group);
  set have;
  by group;
  counter + 1;
  if not firstflag and transaction = '7400'
  then do;
    output;
    firstflag = 1;
  end;
  if transaction in (7470,4320) then lastcount = counter;
end;
counter = 0;
do until (last.group);
  set have;
  by group;
  counter + 1;
  if counter = lastcount then output;
end;
drop firstflag lastcount counter;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;group    sequence    transaction

  A         101          7400   
  A         103          7470   
  B         201          7400   
  B         206          7470   
  C         301          7400   
  C         304          7470   
  D         401          7400   
  D         408          7470   
  E         500          7400   
  E         504          7470   
&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Jul 2019 07:42:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/571708#M161304</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-08T07:42:25Z</dc:date>
    </item>
    <item>
      <title>Re: Finding an observation in a sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/572624#M161612</link>
      <description>Thank you for sharing the code. I am getting the expected result for Group A, B,C and D now. For group E, expecting below result. I need to consider transaction date also. Group E having jan10 and jan11 as transaction date. Based on the date also the 7400 and 4320/7470 transaction matching should happen&lt;BR /&gt;&lt;BR /&gt;E 500 7400 jan10&lt;BR /&gt;E 502 4320 jan10&lt;BR /&gt;E 503 7400 jan11&lt;BR /&gt;E 504 7470 jan11&lt;BR /&gt;</description>
      <pubDate>Thu, 11 Jul 2019 07:48:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/572624#M161612</guid>
      <dc:creator>Divya8</dc:creator>
      <dc:date>2019-07-11T07:48:47Z</dc:date>
    </item>
    <item>
      <title>Re: Finding an observation in a sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/572630#M161615</link>
      <description>&lt;P&gt;So the transaction date (which was not recognized as its own variable in your initial post, where the data step read only three columns) does play a role.&lt;/P&gt;
&lt;P&gt;This only means we have to use two by variables in the crucial step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input group :$1. sequence transaction transaction_date yymmdd10.;
format transaction_date yymmddd10.;
datalines;
A 100 1200 2019-01-10
A 101 7400 2019-01-10
A 102 1000 2019-01-10
A 103 7470 2019-01-10
B 201 7400 2019-01-10
B 202 4044 2019-01-10
B 203 4600 2019-01-10
B 204 7470 2019-01-10
B 205 7400 2019-01-10
B 206 7470 2019-01-10
C 301 7400 2019-01-10
C 302 1000 2019-01-10
C 303 7400 2019-01-10
C 304 7470 2019-01-10
D 500 4060 2019-01-10
D 401 7400 2019-01-10
D 403 4320 2019-01-10
D 404 7400 2019-01-10
D 405 7415 2019-01-10
D 406 7420 2019-01-10
D 407 7400 2019-01-10
D 408 7470 2019-01-10
E 500 7400 2019-01-10
E 501 1000 2019-01-10
E 502 4320 2019-01-10
E 503 7400 2019-01-11
E 504 7470 2019-01-11
;

data want;
firstflag = 0;
lastcount = 0;
counter = 0;
do until (last.transaction_date);
  set have;
  by group transaction_date;
  counter + 1;
  if not firstflag and transaction = '7400'
  then do;
    output;
    firstflag = 1;
  end;
  if transaction in (7470,4320) then lastcount = counter;
end;
counter = 0;
do until (last.transaction_date);
  set have;
  by group transaction_date;
  counter + 1;
  if counter = lastcount then output;
end;
drop firstflag lastcount counter;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;                                    transaction_
group    sequence    transaction        date

  A         101          7400        2019-01-10 
  A         103          7470        2019-01-10 
  B         201          7400        2019-01-10 
  B         206          7470        2019-01-10 
  C         301          7400        2019-01-10 
  C         304          7470        2019-01-10 
  D         401          7400        2019-01-10 
  D         408          7470        2019-01-10 
  E         500          7400        2019-01-10 
  E         502          4320        2019-01-10 
  E         503          7400        2019-01-11 
  E         504          7470        2019-01-11 
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 08:10:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/572630#M161615</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-11T08:10:53Z</dc:date>
    </item>
    <item>
      <title>Re: Finding an observation in a sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/572646#M161620</link>
      <description>Thank you so much. The code is working&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt; I ran a testing with actual data that is given in the project. I found one more scenario where group not having matching 7470/4320 transaction for 7400 transaction. I should not consider this group. For eg&lt;BR /&gt;Group sequence Transaction date&lt;BR /&gt;F 701 7400 2019-01-10&lt;BR /&gt;F 702 1000 2019-01-10&lt;BR /&gt;F 703. 3240 2019-01-10</description>
      <pubDate>Thu, 11 Jul 2019 10:21:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/572646#M161620</guid>
      <dc:creator>Divya8</dc:creator>
      <dc:date>2019-07-11T10:21:21Z</dc:date>
    </item>
    <item>
      <title>Re: Finding an observation in a sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/572655#M161623</link>
      <description>I tried the below option. After extracting the data using the above suggested method, I used the matching check the one I posted it initially. This gives the result&lt;BR /&gt;&lt;BR /&gt;data WANT;&lt;BR /&gt;set HAVE;&lt;BR /&gt;by GROUP TRANSACTION notsorted;&lt;BR /&gt;where TRANSACTION in (7400,7470,4320);&lt;BR /&gt;if (last.TRANSACTION and TRANSACTION=7400 and not last.GROUP)&lt;BR /&gt;or (first.TRANSACTION and TRANSACTION in(7470,4320) and not first.GROUP);&lt;BR /&gt;run;</description>
      <pubDate>Thu, 11 Jul 2019 10:45:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-an-observation-in-a-sas/m-p/572655#M161623</guid>
      <dc:creator>Divya8</dc:creator>
      <dc:date>2019-07-11T10:45:05Z</dc:date>
    </item>
  </channel>
</rss>

