<?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: Selecting 1st Observation Based on Multiple Variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798727#M313997</link>
    <description>Aha! Well I think I found a better approach to what I need. Since there could be multiple transactions per account, I don't want to take just the 1st observation. Here is what I did instead, and seemed to give me what I'm after:&lt;BR /&gt;&lt;BR /&gt;proc sort data=work.final_decision;&lt;BR /&gt;by score_customer_account_xid transaction_dttm transaction_amt cod_mot_rsps_tran;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=work.final_decision out=work.final_decision_nodups nodupkey;&lt;BR /&gt;by score_customer_account_xid transaction_dttm;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Thanks for your help!&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Fri, 25 Feb 2022 19:11:07 GMT</pubDate>
    <dc:creator>lujo1017</dc:creator>
    <dc:date>2022-02-25T19:11:07Z</dc:date>
    <item>
      <title>Selecting 1st Observation Based on Multiple Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798685#M313973</link>
      <description>&lt;P&gt;I have a dataset that that appears as below. There may be multiple COD_MOT_RSPS_TRAN codes for the same SCORE_CUSTOMER_ACCOUNT_XID, TRANSACTION_DTTM and TRANSACTION_AMT:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="lujo1017_0-1645810473088.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68927i5957B4A7FAC63AFC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="lujo1017_0-1645810473088.png" alt="lujo1017_0-1645810473088.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I sorted with the following code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;proc sort data=work.final_decision;&lt;BR /&gt;by score_customer_account_xid transaction_dttm transaction_amt cod_mot_rsps_tran;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to do next is to create another dataset that gives me only the 1st observation by COD_MOT_RSPS_TRN if there are multiple and all other column values are equal (account, datetime, and amount fields).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As an example, here are two observations with the same account and timestamp, but with 2 different COD_MOS_RSPS_TRN values. In the resulting dataset, I would want to keep the observation associated with FL5 since that would would be the 1st record after sorting:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="lujo1017_1-1645810527260.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68928iA05EDE7496C7B590/image-size/medium?v=v2&amp;amp;px=400" role="button" title="lujo1017_1-1645810527260.png" alt="lujo1017_1-1645810527260.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I attempted to accomplish this with the below code, but it removed far too many records (e.g. the example account above was completely removed from the dataset):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;data work.final_decision_nodups;&lt;BR /&gt;set work.final_decision;&lt;BR /&gt;by score_customer_account_xid transaction_dttm transaction_amt cod_mot_rsps_tran;&lt;BR /&gt;if first.cod_mot_rsps_tran then;&lt;BR /&gt;else output;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone tell me what I'm doing incorrectly? I'm on SAS EG 7.1.&amp;nbsp; Thanks!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 17:36:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798685#M313973</guid>
      <dc:creator>lujo1017</dc:creator>
      <dc:date>2022-02-25T17:36:06Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting 1st Observation Based on Multiple Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798687#M313975</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.final_decision_nodups;
    set work.final_decision;
    by score_customer_account_xid transaction_dttm transaction_amt 
    cod_mot_rsps_tran;
    if first.cod_mot_rsps_tran then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In this case, your IF THEN ELSE does not perform the task you wanted it to perform. You said "&lt;SPAN&gt;I want to ... create another dataset that gives me only the 1st observation by COD_MOT_RSPS_TRN". So your IF statement ought to mirror those words ... which ought to translate almost directly in SAS syntax. And so the adjustment to your code is above.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 17:54:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798687#M313975</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-02-25T17:54:48Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting 1st Observation Based on Multiple Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798692#M313978</link>
      <description>&lt;P&gt;Thank you for your reply, but seems like something is still missing. When I run the code you suggest, then query against it for my example account, I receive the exact same output as from work.final_decision:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="lujo1017_0-1645811744993.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68929iAAB00A3693BB6E6D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="lujo1017_0-1645811744993.png" alt="lujo1017_0-1645811744993.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 17:55:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798692#M313978</guid>
      <dc:creator>lujo1017</dc:creator>
      <dc:date>2022-02-25T17:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting 1st Observation Based on Multiple Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798697#M313981</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/417491"&gt;@lujo1017&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for your reply, but seems like something is still missing. When I run the code you suggest, then query against it for my example account, I receive the exact same output as from work.final_decision:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="lujo1017_0-1645811744993.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68929iAAB00A3693BB6E6D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="lujo1017_0-1645811744993.png" alt="lujo1017_0-1645811744993.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why is this not the correct answer? It looks right to me. You get the first record for each value of COD_MOT_RSPS_TRAN, which is exactly what you asked for. You said:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;What I want to do next is to create another dataset that gives me only the 1st observation by COD_MOT_RSPS_TRN if there are multiple and all other column values are equal (account, datetime, and amount fields).&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Fri, 25 Feb 2022 18:00:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798697#M313981</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-02-25T18:00:32Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting 1st Observation Based on Multiple Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798700#M313982</link>
      <description>&lt;P&gt;My apologies, as I don't think I explained correctly. Using the example, I only want to keep the FL5 record. If the values in all the other columns are duplicates, I don't want multiple values for&amp;nbsp;&lt;SPAN&gt;COD_MOT_RSPS_TRAN. I only want the 1st one.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 18:04:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798700#M313982</guid>
      <dc:creator>lujo1017</dc:creator>
      <dc:date>2022-02-25T18:04:48Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting 1st Observation Based on Multiple Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798702#M313984</link>
      <description>&lt;P&gt;Ok, got it. So instead of the code I provided earlier, you want to use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.final_decision_nodups;
    set work.final_decision;
    by score_customer_account_xid transaction_dttm transaction_amt cod_mot_rsps_tran;
    if first._________________ then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;where (homework assignment) you fill in the blank.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 18:13:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798702#M313984</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-02-25T18:13:58Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting 1st Observation Based on Multiple Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798727#M313997</link>
      <description>Aha! Well I think I found a better approach to what I need. Since there could be multiple transactions per account, I don't want to take just the 1st observation. Here is what I did instead, and seemed to give me what I'm after:&lt;BR /&gt;&lt;BR /&gt;proc sort data=work.final_decision;&lt;BR /&gt;by score_customer_account_xid transaction_dttm transaction_amt cod_mot_rsps_tran;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=work.final_decision out=work.final_decision_nodups nodupkey;&lt;BR /&gt;by score_customer_account_xid transaction_dttm;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Thanks for your help!&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 25 Feb 2022 19:11:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-1st-Observation-Based-on-Multiple-Variables/m-p/798727#M313997</guid>
      <dc:creator>lujo1017</dc:creator>
      <dc:date>2022-02-25T19:11:07Z</dc:date>
    </item>
  </channel>
</rss>

