<?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: Looking back values of variables within one year in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820874#M324044</link>
    <description>&lt;P&gt;Thank you so much. Works well&lt;/P&gt;</description>
    <pubDate>Wed, 29 Jun 2022 08:45:24 GMT</pubDate>
    <dc:creator>DathanMD</dc:creator>
    <dc:date>2022-06-29T08:45:24Z</dc:date>
    <item>
      <title>Looking back values of variables within one year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820859#M324031</link>
      <description>&lt;P&gt;Hi SAS experts,&lt;/P&gt;
&lt;P&gt;I am SAS starter and I need your help with some code. I have a dataset of students (id) and their weights over a period (date). I would like to create another variable (Lag_weight_year) that records weight that is more than one year ago but within two years of the current date (date). So, in essence, I will want weights between one year-two year prior to the current date&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;*The data I have;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Data have;&lt;BR /&gt;Format dates date9.;&lt;BR /&gt;input Id Dates:date9. weight;&lt;BR /&gt;cards;&lt;BR /&gt;1 3/01/2012 64 &lt;BR /&gt;1 29/05/2012 87 &lt;BR /&gt;1 16/10/2012 87 &lt;BR /&gt;1 2/04/2013 88&lt;BR /&gt;2 20/08/2013 88&lt;BR /&gt;2 4/02/2014 88&lt;BR /&gt;2 13/05/2014 90&lt;BR /&gt;2 15/07/2014 90&lt;BR /&gt;2 14/12/2015 78&lt;BR /&gt;2 16/12/2015 76&lt;BR /&gt;2 14/02/2018 76&lt;BR /&gt;3 3/01/2012 85&lt;BR /&gt;3 29/05/2013 64&lt;BR /&gt;3 16/10/2014 87&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;*The dataset i want (want);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; want;&lt;/P&gt;
&lt;P&gt;Format dates date9.;&lt;/P&gt;
&lt;P&gt;input Id Dates:date9. weight Lag_weight_year;&lt;/P&gt;
&lt;P&gt;cards;&lt;/P&gt;
&lt;P&gt;1 3/01/2012 64 .&lt;/P&gt;
&lt;P&gt;1 29/05/2012 87 .&lt;/P&gt;
&lt;P&gt;1 16/10/2012 87 .&lt;/P&gt;
&lt;P&gt;1 2/04/2013 88 64&lt;/P&gt;
&lt;P&gt;2 20/08/2013 88 .&lt;/P&gt;
&lt;P&gt;2 4/02/2014 88 .&lt;/P&gt;
&lt;P&gt;2 13/05/2014 90 88&lt;/P&gt;
&lt;P&gt;2 15/07/2014 90 88&lt;/P&gt;
&lt;P&gt;2 14/12/2015 78 90&lt;/P&gt;
&lt;P&gt;2 16/12/2015 76 90&lt;/P&gt;
&lt;P&gt;2 14/02/2018 76 .&lt;/P&gt;
&lt;P&gt;3 3/01/2012 85 .&lt;/P&gt;
&lt;P&gt;3 29/05/2013 64 85&lt;/P&gt;
&lt;P&gt;3 16/10/2014 87 64&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dathan Byonanebye&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 05:00:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820859#M324031</guid>
      <dc:creator>DathanMD</dc:creator>
      <dc:date>2022-06-29T05:00:34Z</dc:date>
    </item>
    <item>
      <title>Re: Looking back values of variables within one year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820862#M324034</link>
      <description>&lt;P&gt;If a current date has multiple prior dates that fall between 1 and 2 years prior, which of those prior weights do you want?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;editted additional question:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, why does the third record of ID=2 (13/05/2014) in your expected output have a lag_weight_value=88?&amp;nbsp; Neither of the two preceding dates (20/08/2013 and 4/02/2014) are more than 1 year prior.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 05:35:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820862#M324034</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-06-29T05:35:40Z</dc:date>
    </item>
    <item>
      <title>Re: Looking back values of variables within one year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820863#M324035</link>
      <description>&lt;P&gt;Thanks for asking. In that case, i would like the latest of those multiple dates. The one closest to the current date&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 05:21:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820863#M324035</guid>
      <dc:creator>DathanMD</dc:creator>
      <dc:date>2022-06-29T05:21:43Z</dc:date>
    </item>
    <item>
      <title>Re: Looking back values of variables within one year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820864#M324036</link>
      <description>This is my oversight, ignore that entry</description>
      <pubDate>Wed, 29 Jun 2022 05:38:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820864#M324036</guid>
      <dc:creator>DathanMD</dc:creator>
      <dc:date>2022-06-29T05:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: Looking back values of variables within one year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820865#M324037</link>
      <description>&lt;P&gt;By "between one year-two year prior" I assume you mean any date from&amp;nbsp;exactly one year prior up to but not including exactly two years prior.&amp;nbsp; That range includes all cases in which the function&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;intck('year',from_date,to_date,'continuous')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;returns a value of 1.&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;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Format dates date9.;
input Id Dates:ddmmyy10. weight;
cards;
1 3/01/2012 64
1 29/05/2012 87
1 16/10/2012 87
1 2/04/2013 88
2 20/08/2013 88
2 4/02/2014 88
2 13/05/2014 90
2 15/07/2014 90
2 14/12/2015 78
2 16/12/2015 76
2 14/02/2018 76
3 3/01/2012 85
3 29/05/2013 64
3 16/10/2014 87
Run;

data want (drop=_:);
  set have;
  by id;
  array date_history {20} _temporary_;
  array weight_history {20} _temporary_;
  if first.id then do;
    call missing(of weight_history{*}, of date_history{*});
    _seq=1;
  end;
  else _seq+1;

  weight_history{_seq}=weight;
  date_history{_seq}=dates;

  if _seq&amp;gt;1 then do _s=_seq-1 to 1 by -1 until (intck('year',date_history{_s},dates,'continuous')=1);
  end;
  if _s&amp;gt;0 then lag_weight_year=weight_history{_s};
  if _s&amp;gt;0 then date_of_lag_weight_year=date_history{_s};
  format date_of_lag_weight_year date9.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you for providing sample data in the format of a data step.&amp;nbsp; It wasn't quite a &lt;EM&gt;&lt;STRONG&gt;working&lt;/STRONG&gt;&lt;/EM&gt; data step, but changing the informat used for variable dates in the input statement fixed it.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 05:51:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820865#M324037</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-06-29T05:51:15Z</dc:date>
    </item>
    <item>
      <title>Re: Looking back values of variables within one year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820874#M324044</link>
      <description>&lt;P&gt;Thank you so much. Works well&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 08:45:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-back-values-of-variables-within-one-year/m-p/820874#M324044</guid>
      <dc:creator>DathanMD</dc:creator>
      <dc:date>2022-06-29T08:45:24Z</dc:date>
    </item>
  </channel>
</rss>

