<?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 construct a new variable by comparing fields in two data sets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844573#M333903</link>
    <description>&lt;P&gt;Sure thing. If it works, please mark the code above the accepted answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Nov 2022 07:53:13 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2022-11-16T07:53:13Z</dc:date>
    <item>
      <title>How to construct a new variable by comparing fields in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844040#M333696</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I have two data sets, one is claim level data, and the other is (long-term or inpatient) stay level data. I wanted to construct a new field for the stay level data based on the claim level data. For example, when a diagnosis (flag_dx) showed up in a claim during the claim service dates (clm_beg_dt and clm_end_dt), and if the claim service dates are within the stay time window, the stay should take a value 1. Otherwise, the stay has the value of 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;The claim level data is as below:&lt;/P&gt;
&lt;P&gt;data clmfile;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID clm_beg_dt :mmddyy. clm_end_dt :mmddyy. flag_dx;&lt;BR /&gt;format clm_beg_dt clm_end_dt mmddyy.;&lt;BR /&gt;datalines;&lt;BR /&gt;1,4/17/2017,11/30/2017,0&lt;BR /&gt;1,4/17/2017,12/31/2017,0&lt;BR /&gt;1,4/17/2017,4/30/2018,0&lt;BR /&gt;1,4/17/2017,6/30/2018,0&lt;BR /&gt;1,4/17/2017,8/31/2018,0&lt;BR /&gt;1,4/17/2017,10/31/2018,0&lt;BR /&gt;1,4/17/2017,12/31/2018,0&lt;BR /&gt;2,7/17/2017,7/20/2017,0&lt;BR /&gt;2,7/20/2017,7/31/2017,0&lt;BR /&gt;2,8/1/2017,8/5/2017,0&lt;BR /&gt;2,6/7/2018,6/10/2018,0&lt;BR /&gt;2,8/2/2018,8/5/2018,1&lt;BR /&gt;2,8/5/2018,8/14/2018,1&lt;BR /&gt;2,8/14/2018,8/19/2018,1&lt;BR /&gt;2,8/19/2018,8/31/2018,1&lt;BR /&gt;2,8/19/2018,9/30/2018,1&lt;BR /&gt;2,8/19/2018,10/31/2018,1&lt;BR /&gt;2,8/19/2018,11/30/2022,1&lt;BR /&gt;2,8/19/2018,12/31/2018,1&lt;BR /&gt;2,9/1/2018,9/5/2018,1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;The stay level data file is as below:&lt;/P&gt;
&lt;P&gt;data stayfile;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID Stay_ID Stay_from_dt :mmddyy. Stay_Thru_dt :mmddyy.;&lt;BR /&gt;format Stay_from_dt Stay_Thru_dt mmddyy.;&lt;BR /&gt;datalines;&lt;BR /&gt;1,1,4/17/2017,12/31/2018&lt;BR /&gt;2,1,7/20/2017,8/5/2017&lt;BR /&gt;2,2,6/10/2018,6/29/2018&lt;BR /&gt;2,3,8/5/2018,12/31/2018&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The target data file is as below:&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID Stay_ID Stay_from_dt :mmddyy. Stay_Thru_dt :mmddyy. flag_dx;&lt;BR /&gt;format Stay_from_dt Stay_Thru_dt mmddyy.;&lt;BR /&gt;datalines;&lt;BR /&gt;1,1,4/17/2017,12/31/2018,0&lt;BR /&gt;2,1,7/20/2017,8/5/2017,0&lt;BR /&gt;2,2,6/10/2018,6/29/2018,0&lt;BR /&gt;2,3,8/5/2018,12/31/2018,1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Any suggestion is greatly appreciated!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2022 02:54:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844040#M333696</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2022-11-14T02:54:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to construct a new variable by comparing fields in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844169#M333736</link>
      <description>I was thinking one possible way is to join the two datasets by Person_ID and then compare the two sets of dates. However, the join would be many-to-many join, which is not efficient.</description>
      <pubDate>Mon, 14 Nov 2022 17:05:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844169#M333736</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2022-11-14T17:05:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to construct a new variable by comparing fields in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844172#M333737</link>
      <description>&lt;P&gt;What if one of the observations in the claim data set has flag 0 for just 1 of the 'in-between' obs?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Like the obs below. What if flag was 0 instead?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2,8/19/2018,9/30/2018,1&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2022 18:35:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844172#M333737</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-11-14T18:35:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to construct a new variable by comparing fields in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844174#M333738</link>
      <description>I believe you meant the case as below. Instead of having 1 all the way through for the time period of stay_ID 3, Person 2 had some 0s during its Stay 3 between 8/5/2018 and 12/31/2018. This case I only care about if flag_dx=1 ever showed. If flag_dx=1 ever showed once during a stay within the stay time period, the stay for the person should have flag_dx=1 in the target data set. &lt;BR /&gt;2,8/2/2018,8/5/2018,1&lt;BR /&gt;2,8/5/2018,8/14/2018,1&lt;BR /&gt;2,8/14/2018,8/19/2018,0&lt;BR /&gt;2,8/19/2018,8/31/2018,1&lt;BR /&gt;2,8/19/2018,9/30/2018,1&lt;BR /&gt;2,8/19/2018,10/31/2018,0&lt;BR /&gt;2,8/19/2018,11/30/2022,1&lt;BR /&gt;2,8/19/2018,12/31/2018,1&lt;BR /&gt;2,9/1/2018,9/5/2018,1&lt;BR /&gt;Thanks a lot!</description>
      <pubDate>Mon, 14 Nov 2022 18:50:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844174#M333738</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2022-11-14T18:50:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to construct a new variable by comparing fields in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844176#M333740</link>
      <description>&lt;P&gt;Ok. And to fully understand: The date interval in the claims data must be completely enclosed by the date interval in&amp;nbsp;stayfile, right? Meaning that&amp;nbsp;clm_beg_dt &amp;gt;=&amp;nbsp;Stay_from_dt and&amp;nbsp;clm_end_dt &amp;lt;=&amp;nbsp;Stay_Thru_dt ?&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2022 18:55:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844176#M333740</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-11-14T18:55:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to construct a new variable by comparing fields in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844180#M333742</link>
      <description>That's right. I should have indicated that in my initial ask. Stay_from_dt&amp;lt;=clm_beg_dt&amp;lt;= clm_end_dt&amp;lt;=Stay_Thru_dt</description>
      <pubDate>Mon, 14 Nov 2022 19:01:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844180#M333742</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2022-11-14T19:01:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to construct a new variable by comparing fields in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844184#M333745</link>
      <description>&lt;P&gt;Ok. Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop = clm_beg_dt clm_end_dt flag_dx);

   if _N_ = 1 then do;
      dcl hash h(dataset : 'clmfile', multidata : 'Y');
      h.definekey('Person_ID');
      h.definedata(all : 'Y');
      h.definedone();
   end;

   set stayfile;

   if 0 then set clmfile;
   call missing(clm_beg_dt, clm_end_dt, flag_dx);
   flag = 0;

   do while (h.do_over() = 0);
      if clm_beg_dt &amp;gt;= Stay_from_dt 
     and clm_end_dt &amp;lt;= Stay_Thru_dt 
     and flag_dx = 1 then do;
        flag = 1;
        leave;
      end;
   end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Person_ID  Stay_ID  Stay_from_dt  Stay_Thru_dt  flag
1          1        04/17/17      12/31/18      0
2          1        07/20/17      08/05/17      0
2          2        06/10/18      06/29/18      0
2          3        08/05/18      12/31/18      1&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2022 19:05:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844184#M333745</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-11-14T19:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to construct a new variable by comparing fields in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844447#M333840</link>
      <description>&lt;P&gt;Thank you, Peter!&lt;BR /&gt;I tried out the code with real data. When there are Person_IDs with flag_dx=0 following Person_IDs with flag_dx=1, the Person_IDs with flag_dx=0 would be replaced with Person_IDs with flag_dx=1. &lt;BR /&gt;For example, when there is third person,&amp;nbsp;Person_ID=3, with flag_dx=0, Person_ID would become Person_ID=2. The data below would have an unexpected output using the code you provided. Any further insight?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data clmfile;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID clm_beg_dt :mmddyy. clm_end_dt :mmddyy. flag_dx;&lt;BR /&gt;format clm_beg_dt clm_end_dt mmddyy.;&lt;BR /&gt;datalines;&lt;BR /&gt;1,4/17/2017,11/30/2017,0&lt;BR /&gt;1,4/17/2017,12/31/2017,0&lt;BR /&gt;1,4/17/2017,4/30/2018,0&lt;BR /&gt;1,4/17/2017,6/30/2018,0&lt;BR /&gt;1,4/17/2017,8/31/2018,0&lt;BR /&gt;1,4/17/2017,10/31/2018,0&lt;BR /&gt;1,4/17/2017,12/31/2018,0&lt;BR /&gt;2,7/17/2017,7/20/2017,0&lt;BR /&gt;2,7/20/2017,7/31/2017,0&lt;BR /&gt;2,8/1/2017,8/5/2017,0&lt;BR /&gt;2,6/7/2018,6/10/2018,0&lt;BR /&gt;2,8/2/2018,8/5/2018,1&lt;BR /&gt;2,8/5/2018,8/14/2018,1&lt;BR /&gt;2,8/14/2018,8/19/2018,1&lt;BR /&gt;2,8/19/2018,8/31/2018,1&lt;BR /&gt;2,8/19/2018,9/30/2018,1&lt;BR /&gt;2,8/19/2018,10/31/2018,1&lt;BR /&gt;2,8/19/2018,11/30/2022,1&lt;BR /&gt;2,8/19/2018,12/31/2018,1&lt;BR /&gt;2,9/1/2018,9/5/2018,1&lt;BR /&gt;3,12/7/2017,12/12/2017,0&lt;BR /&gt;3,12/12/2017,12/31/2017,0&lt;BR /&gt;3,1/1/2018,1/16/2018,0&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data stayfile;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID Stay_ID Stay_from_dt :mmddyy. Stay_Thru_dt :mmddyy.;&lt;BR /&gt;format Stay_from_dt Stay_Thru_dt mmddyy.;&lt;BR /&gt;datalines;&lt;BR /&gt;1,1,4/17/2017,12/31/2018&lt;BR /&gt;2,1,7/20/2017,8/5/2017&lt;BR /&gt;2,2,6/10/2018,6/29/2018&lt;BR /&gt;2,3,8/5/2018,12/31/2018&lt;BR /&gt;3,1,12/12/2017,1/16/2018&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2022 18:22:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844447#M333840</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2022-11-15T18:22:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to construct a new variable by comparing fields in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844475#M333854</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/430334"&gt;@lichee&lt;/a&gt;&amp;nbsp;I see &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Try this instead. Let me know if it works for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop = clm_beg_dt clm_end_dt flag_dx);

   if _N_ = 1 then do;
      dcl hash h(dataset : 'clmfile', multidata : 'Y');
      h.definekey('Person_ID');
      h.definedata(all : 'Y');
      h.definedone();
   end;

   set stayfile;

   if 0 then set clmfile;
   call missing(clm_beg_dt, clm_end_dt, flag_dx);
   flag = 0;

   do while (h.do_over() = 0);
      if clm_beg_dt &amp;gt;= Stay_from_dt 
     and clm_end_dt &amp;lt;= Stay_Thru_dt 
     and flag_dx = 1 then do;
        flag = 1;
      end;
   end;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Nov 2022 19:53:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844475#M333854</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-11-15T19:53:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to construct a new variable by comparing fields in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844502#M333864</link>
      <description>This one works well! I'll need to do some reading to learn about hash object now. Thanks a lot!</description>
      <pubDate>Tue, 15 Nov 2022 21:33:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844502#M333864</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2022-11-15T21:33:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to construct a new variable by comparing fields in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844573#M333903</link>
      <description>&lt;P&gt;Sure thing. If it works, please mark the code above the accepted answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2022 07:53:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-construct-a-new-variable-by-comparing-fields-in-two-data/m-p/844573#M333903</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-11-16T07:53:13Z</dc:date>
    </item>
  </channel>
</rss>

