<?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: Set the same Index for repeated rows (dates) in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933788#M41966</link>
    <description>&lt;P&gt;Are you saying you want to new variable to be TRUE when there are multiple observations for the same ID and admission date?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Index;
  format Admission Discharge date9.;
cards;
0001  13JAN2017 25JAN2017  0 
0001  13JAN2017 25JAN2017  1
0001  22FEB2018 03MAR2018  0   
0001  30JAN2019 04MAR2019  0  
0002  01DEC2018 14DEC2018  1 
0002  01DEC2018 14DEC2018  0 
0003  09JAN2016 25JAN2016  1 
0003  29JAN2018 12FEB2018  0
; 

data want;
  set db ;
  by id admission ;
  new_flag = not (first.admission and last.admission);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs     ID     Admission    Discharge    Index    new_flag

 1     0001    13JAN2017    25JAN2017      0          1
 2     0001    13JAN2017    25JAN2017      1          1
 3     0001    22FEB2018    03MAR2018      0          0
 4     0001    30JAN2019    04MAR2019      0          0
 5     0002    01DEC2018    14DEC2018      1          1
 6     0002    01DEC2018    14DEC2018      0          1
 7     0003    09JAN2016    25JAN2016      1          0
 8     0003    29JAN2018    12FEB2018      0          0
&lt;/PRE&gt;</description>
    <pubDate>Wed, 26 Jun 2024 13:44:33 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-06-26T13:44:33Z</dc:date>
    <item>
      <title>Set the same Index for repeated rows (dates)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933770#M41964</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following dataset DB. Is there a way to get DB1? In other words where there are repeated (identical) dates the Index should be 1. NB: all IDs have only one record (row) where Index = 1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Index;
  format Admission date9. Discharge date9.;
cards;
0001  13JAN2017 25JAN2017  0 
0001  13JAN2017 25JAN2017  1
0001  22FEB2018 03MAR2018  0   
0001  30JAN2019 04MAR2019  0  
0002  01DEC2018 14DEC2018  1 
0002  01DEC2018 14DEC2018  0 
0003  09JAN2016 25JAN2016  1 
0003  29JAN2018 12FEB2018  0; 
run;


data DB1;
  input ID :$20. Admission :date09. Discharge :date09. Index;
  format Admission date9. Discharge date9.;
cards;
0001  13JAN2017 25JAN2017  1 
0001  13JAN2017 25JAN2017  1
0001  22FEB2018 03MAR2018  0   
0001  30JAN2019 04MAR2019  0  
0002  01DEC2018 14DEC2018  1 
0002  01DEC2018 14DEC2018  1 
0003  09JAN2016 25JAN2016  1 
0003  29JAN2018 12FEB2018  0; 
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2024 10:20:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933770#M41964</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-06-26T10:20:05Z</dc:date>
    </item>
    <item>
      <title>Re: Set the same Index for repeated rows (dates)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933772#M41965</link>
      <description>&lt;P&gt;We have already shown you solutions on two very similar data sets in these threads: (&lt;A href="https://communities.sas.com/t5/New-SAS-User/Remove-duplicated-dates-based-on-an-Index-value/m-p/932976" target="_self"&gt;thread 1&lt;/A&gt; &lt;A href="https://communities.sas.com/t5/New-SAS-User/Update-values-of-a-variable-based-on-another-variable/td-p/932535" target="_self"&gt;thread 2&lt;/A&gt;)&lt;/P&gt;
&lt;P&gt;Can you show us what you have tried for this problem?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2024 11:20:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933772#M41965</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-06-26T11:20:07Z</dc:date>
    </item>
    <item>
      <title>Re: Set the same Index for repeated rows (dates)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933788#M41966</link>
      <description>&lt;P&gt;Are you saying you want to new variable to be TRUE when there are multiple observations for the same ID and admission date?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Index;
  format Admission Discharge date9.;
cards;
0001  13JAN2017 25JAN2017  0 
0001  13JAN2017 25JAN2017  1
0001  22FEB2018 03MAR2018  0   
0001  30JAN2019 04MAR2019  0  
0002  01DEC2018 14DEC2018  1 
0002  01DEC2018 14DEC2018  0 
0003  09JAN2016 25JAN2016  1 
0003  29JAN2018 12FEB2018  0
; 

data want;
  set db ;
  by id admission ;
  new_flag = not (first.admission and last.admission);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs     ID     Admission    Discharge    Index    new_flag

 1     0001    13JAN2017    25JAN2017      0          1
 2     0001    13JAN2017    25JAN2017      1          1
 3     0001    22FEB2018    03MAR2018      0          0
 4     0001    30JAN2019    04MAR2019      0          0
 5     0002    01DEC2018    14DEC2018      1          1
 6     0002    01DEC2018    14DEC2018      0          1
 7     0003    09JAN2016    25JAN2016      1          0
 8     0003    29JAN2018    12FEB2018      0          0
&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jun 2024 13:44:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933788#M41966</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-26T13:44:33Z</dc:date>
    </item>
    <item>
      <title>Re: Set the same Index for repeated rows (dates)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933815#M41968</link>
      <description>Hi Tom thank you for your help. &lt;BR /&gt;I tried to apply your code but there are cases where dates are duplicated and Index = 0 that with your code become flagged 1 while they will stay = 0.</description>
      <pubDate>Wed, 26 Jun 2024 15:49:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933815#M41968</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-06-26T15:49:34Z</dc:date>
    </item>
    <item>
      <title>Re: Set the same Index for repeated rows (dates)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933821#M41969</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi Tom thank you for your help. &lt;BR /&gt;I tried to apply your code but there are cases where dates are duplicated and Index = 0 that with your code become flagged 1 while they will stay = 0.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So you need to actually explain what the rule is for calculating this new variable.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2024 16:56:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933821#M41969</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-26T16:56:45Z</dc:date>
    </item>
    <item>
      <title>Re: Set the same Index for repeated rows (dates)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933838#M41970</link>
      <description>So if the dates are duplicated and at least for one record  Index =1 then also index should be duplicated to be 1 otherwise nothing to do.</description>
      <pubDate>Wed, 26 Jun 2024 17:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933838#M41970</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-06-26T17:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: Set the same Index for repeated rows (dates)</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933844#M41971</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;So if the dates are duplicated and at least for one record Index =1 then also index should be duplicated to be 1 otherwise nothing to do.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So if any of the observations in the group have a 1 then value should be one?&lt;/P&gt;
&lt;P&gt;That is essentially a MAX() operation across all of the observations in the group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table want as 
  select DB.ID
       , DB.Admission
       , DB.Discharge
       , DB.Index
       , max(DB.Index) as New_Index
  from DB
  group by DB.ID
         , DB.Admission
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1719427378124.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97920iB63B446D4F2B0128/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1719427378124.png" alt="Tom_0-1719427378124.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: You might need to make new test data if you want to actually test this new logic.&amp;nbsp; For example a group of observations with the same ID and admission date where none of them currently have INDEX=1.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2024 18:45:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Set-the-same-Index-for-repeated-rows-dates/m-p/933844#M41971</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-26T18:45:00Z</dc:date>
    </item>
  </channel>
</rss>

