<?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: Move values of variables based on replicated dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Move-values-of-variables-based-on-replicated-dates/m-p/950234#M371615</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: "Variable1 =1 always at the first..." is a statement. Replicated rows are identical: for ID, Admission and Discharge.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Still do not know what that means, but since your example is not changing the value of VARIABLE1 perhaps we can just ignore it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If we assume that the source data has a maximum of one observation per group that has VARIABLE1 set to 1.&amp;nbsp; And if we assume that your goal is to modify the values of VARIABLE2 only in the groups that ever have VARIABLE2 set to 1 then perhaps this SQL query is what you want?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table db1 as
select id ,admission ,discharge ,variable1
     , case when (max(variable2)) then variable1
            else variable2 
       end as variable2
from db
group by id, admission, discharge
order by id, admission, discharge, variable1 desc 
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SELECT statement lists the variables to create in the NEW dataset. We copy most of the existing variables as they are.&amp;nbsp; Only VARIABLE2 needs some change.&amp;nbsp; So when vairable2 is ever true for the group then use the values of variable1 (so that it has 1 on the first and zero everywhere else) otherwise leave variable2 as it was.&lt;/P&gt;
&lt;P&gt;Make sure to order the repeating observations by descending value of VARIABLE1.&lt;/P&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1731083259618.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102044i189E28A6E4C18356/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1731083259618.png" alt="Tom_0-1731083259618.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>Fri, 08 Nov 2024 16:31:19 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-11-08T16:31:19Z</dc:date>
    <item>
      <title>Move values of variables based on replicated dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-values-of-variables-based-on-replicated-dates/m-p/950205#M371607</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Variable1 Variable2;
cards;
166  16FEB2019 26FEB2019  1   0
170  22FEB2017 07MAR2017  1   0
170  22FEB2017 07MAR2017  0   1
170  30JAN2019 04MAR2019  0   0
313  03MAR2016 10MAR2016  1   0
313  03MAR2016 10MAR2016  0   1
313  12DEC2019 15DEC2019  1   0
215  22DEC2014 25DEC2014  1   1
; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a way to get the following?&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB1;
  input ID :$20. Admission :date09. Discharge :date09. Variable1 Variable2;
cards;
166  16FEB2019 26FEB2019  1   0
170  22FEB2017 07MAR2017  1   1
170  22FEB2017 07MAR2017  0   0
170  30JAN2019 04MAR2019  0   0
313  03MAR2016 10MAR2016  1   1
313  03MAR2016 10MAR2016  0   0
313  12DEC2019 15DEC2019  1   0
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In other words (referring ONLY to replicated dates) if Variable1 =1 and Variable2 = 0 but Variable2 =1 for the second replica then move the value of Variable2=1 to the replica where Variable1 = 1.Variable1 = 1 always at the first replica of dates.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much in advance&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2024 13:13:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-values-of-variables-based-on-replicated-dates/m-p/950205#M371607</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-11-08T13:13:53Z</dc:date>
    </item>
    <item>
      <title>Re: Move values of variables based on replicated dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-values-of-variables-based-on-replicated-dates/m-p/950212#M371608</link>
      <description>&lt;P&gt;Your description is a little unclear.&amp;nbsp; It would help if you gave the variables more meaningful names to reflect what they contain.&amp;nbsp; It would also help (and is probably going to be necessary) to create NEW variables to have the values you want to calculate (not "move").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For now we could call the old variables HAVE1 and HAVE2 and the new variables WANT1 and WANT2.&amp;nbsp; You can always add a rename and/or drop statement to change the names so the new variables end up with the old names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your example does not have the result for the last observation. But let's assume since there are is only one observation that the values do not change.&amp;nbsp; Let's just add your expected result as two extra variables in our example input dataset so it will be easy to detect if we succeed in making them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date. Discharge :date. have1 have2 expect1 expect2;
  format admission discharge date9.;
cards;
166 16FEB2019 26FEB2019 1 0 1 0
170 22FEB2017 07MAR2017 1 0 1 1
170 22FEB2017 07MAR2017 0 1 0 0
170 30JAN2019 04MAR2019 0 0 0 0
313 03MAR2016 10MAR2016 1 0 1 1
313 03MAR2016 10MAR2016 0 1 0 0
313 12DEC2019 15DEC2019 1 0 1 0
215 22DEC2014 25DEC2014 1 1 1 1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What did you mean by:&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Variable1 = 1 always at the first replica of dates.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Is this a statement of how the original variable was defined?&amp;nbsp;Or is it a statement of how you want the new variable defined?&lt;/STRONG&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you mean by replicates?&amp;nbsp; Are you talking about grouping by ID and ADMISSION?&amp;nbsp; Or ID, ADMISSION and DISCHARGE?&amp;nbsp; In your example all of the cases with duplicate ADMISSION dates also have matching DISCHARGE dates.&amp;nbsp; Is that always going to be the case?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2024 14:14:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-values-of-variables-based-on-replicated-dates/m-p/950212#M371608</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-11-08T14:14:21Z</dc:date>
    </item>
    <item>
      <title>Re: Move values of variables based on replicated dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-values-of-variables-based-on-replicated-dates/m-p/950216#M371609</link>
      <description>&lt;P&gt;Hi hope this helps,&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Variable1 Variable2;
cards;
166  16FEB2019 26FEB2019  1   0
170  22FEB2017 07MAR2017  1   0
170  22FEB2017 07MAR2017  0   1
170  30JAN2019 04MAR2019  0   0
313  03MAR2016 10MAR2016  1   0
313  03MAR2016 10MAR2016  0   1
313  12DEC2019 15DEC2019  1   0
215  22DEC2014 25DEC2014  1   1
; 
run;

proc sort;
  by ID Admission Discharge;
run;

/* Step 1: Create a temporary dataset with the maximum value of Variable2 for each combination of ID, Admission, and Discharge */
proc sql;
  create table Temp as
  select ID, Admission, Discharge, max(Variable2) as MaxVar2
  from DB
  group by ID, Admission, Discharge;
quit;

/* Step 2: Merge the temporary dataset back with the original dataset to update Variable2 */
data DB1;
  merge DB(in=a drop=variable2) Temp(in=b);
  by ID Admission Discharge;
  if a;
  if Variable1 = 1 then Variable2 = MaxVar2;
  if cmiss(variable2)=1 then variable2=0;
  drop MaxVar2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Nov 2024 14:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-values-of-variables-based-on-replicated-dates/m-p/950216#M371609</guid>
      <dc:creator>himself</dc:creator>
      <dc:date>2024-11-08T14:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: Move values of variables based on replicated dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-values-of-variables-based-on-replicated-dates/m-p/950221#M371612</link>
      <description>So: "Variable1 =1 always at the first..." is a statement. Replicated rows are identical:  for ID, Admission and Discharge.</description>
      <pubDate>Fri, 08 Nov 2024 15:01:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-values-of-variables-based-on-replicated-dates/m-p/950221#M371612</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-11-08T15:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: Move values of variables based on replicated dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Move-values-of-variables-based-on-replicated-dates/m-p/950234#M371615</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: "Variable1 =1 always at the first..." is a statement. Replicated rows are identical: for ID, Admission and Discharge.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Still do not know what that means, but since your example is not changing the value of VARIABLE1 perhaps we can just ignore it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If we assume that the source data has a maximum of one observation per group that has VARIABLE1 set to 1.&amp;nbsp; And if we assume that your goal is to modify the values of VARIABLE2 only in the groups that ever have VARIABLE2 set to 1 then perhaps this SQL query is what you want?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table db1 as
select id ,admission ,discharge ,variable1
     , case when (max(variable2)) then variable1
            else variable2 
       end as variable2
from db
group by id, admission, discharge
order by id, admission, discharge, variable1 desc 
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SELECT statement lists the variables to create in the NEW dataset. We copy most of the existing variables as they are.&amp;nbsp; Only VARIABLE2 needs some change.&amp;nbsp; So when vairable2 is ever true for the group then use the values of variable1 (so that it has 1 on the first and zero everywhere else) otherwise leave variable2 as it was.&lt;/P&gt;
&lt;P&gt;Make sure to order the repeating observations by descending value of VARIABLE1.&lt;/P&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1731083259618.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102044i189E28A6E4C18356/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1731083259618.png" alt="Tom_0-1731083259618.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>Fri, 08 Nov 2024 16:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Move-values-of-variables-based-on-replicated-dates/m-p/950234#M371615</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-11-08T16:31:19Z</dc:date>
    </item>
  </channel>
</rss>

