<?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: Creating variables that derive their values on a given point on a longitudinal Dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/696112#M212556</link>
    <description>&lt;P&gt;Try this. At a little extra memory cost, but should speed things up.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;10958 and 25932 are the numerical representations of the dates 01Jan1990 and 31Dec2030. You can expand as needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id (a1-a3)(:date9.) click1-click3 review :date9.;
format a: review date9.;
datalines;
1 01Jan2020 05Jan2020 10Jan2020 10 20 30 03Jan2020
2 05Jan2020 10Jan2020 15Jan2020 40 50 60 12Jan2020
3 10Jan2020 15Jan2020 20Jan2020 70 80 90 15Jan2020
;

data want (drop=j);
   set have;
   array d {10958 : 25932} _temporary_;
   array a a:;
   array click click:;
   call missing (of d[*]);

   do over a; d[a] = 1; end;
   j = review;

   do while (1);
      j +- 1;
      if d[j] then leave;
   end;

   _I_ = whichn(j, of a[*]);
   v = click;
   
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 03 Nov 2020 07:55:52 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2020-11-03T07:55:52Z</dc:date>
    <item>
      <title>Creating variables that derive their values on a given point on a longitudinal Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/695898#M212438</link>
      <description>&lt;P&gt;I couldn't really think of an appropriate title which probably doesn't make too much sense but hopefully the following will:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I&amp;nbsp;have an array of variables A1 to A10. Each&amp;nbsp;variable's value is&amp;nbsp;a specific date which relates to a period in time when the customers details were updated on the database.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i.e.&amp;nbsp;A1=21st&amp;nbsp;Dec&amp;nbsp;2018, A2=30th&amp;nbsp;Jan 2019, A3=26th Feb 2019...... etc for customer 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Each of the variables above have similar sequentially labled variables&amp;nbsp;related to the number of customer clicks on a website on the day of each of the A1-A10 dates:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i.e. Click1=50, Click2=40 etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also have a unique review date variable for each customer. These are random dates that fall on any date in the period between A1-A10 dates. I.e. The dates could be values such as 29th Jan 2019, 2nd Feb 2019 etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I am looking to do is create a variable that returns the value of clicks on the closest date before the review date. I.e. If review date is 25th December 2018 for customer 1, I would like to be able to look back to the closest date before it, which would be A1 (21st December 2018) and then return the value of Click 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone know any code which would enable me to do this?&lt;/P&gt;</description>
      <pubDate>Mon, 02 Nov 2020 11:13:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/695898#M212438</guid>
      <dc:creator>EC27556</dc:creator>
      <dc:date>2020-11-02T11:13:40Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variables that derive their values on a given point on a longitudinal Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/695901#M212441</link>
      <description>&lt;P&gt;Please post data in usable form, this will help us to better understand your problem and we have something to work with.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Nov 2020 11:31:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/695901#M212441</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-11-02T11:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variables that derive their values on a given point on a longitudinal Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/695902#M212442</link>
      <description>&lt;P&gt;Perhaps something like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just created some simple example data for demonstration. The code is easily extendable to more variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id (a1-a3)(:date9.) click1-click3 review :date9.;
format a: review date9.;
datalines;
1 01Jan2020 05Jan2020 10Jan2020 10 20 30 03Jan2020
2 05Jan2020 10Jan2020 15Jan2020 40 50 60 12Jan2020
3 10Jan2020 15Jan2020 20Jan2020 70 80 90 15Jan2020
;

data want(drop = d idx);
   set have;
   array a {*} a:;
   array click {*} click:;
   d = review;
   do while (1);
      d +-1;
      idx = whichn(d, of a[*]);
      if idx | d = min(of a[*]) then leave;
   end;
   v = click[idx];
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;id a1        a2        a3        click1 click2 click3 review    v 
1  01JAN2020 05JAN2020 10JAN2020 10     20     30     03JAN2020 10 
2  05JAN2020 10JAN2020 15JAN2020 40     50     60     12JAN2020 50 
3  10JAN2020 15JAN2020 20JAN2020 70     80     90     15JAN2020 70 &lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Nov 2020 11:33:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/695902#M212442</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-02T11:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variables that derive their values on a given point on a longitudinal Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/695904#M212443</link>
      <description>&lt;P&gt;Your dataset is in fact the exact opposite of a longitudinal dataset, and a very poor choice for dataset structure.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;what if you have more than 10 updates?&lt;/LI&gt;
&lt;LI&gt;if you have less than 10 updates, you waste storage space&lt;/LI&gt;
&lt;LI&gt;to deal with a specific date, you need to loop over arrays; SQL does not have such a concept, so you lose SQL as a tool&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;So I recommend that you first transpose to a structure like this&lt;/P&gt;
&lt;PRE&gt;customer date click&lt;/PRE&gt;
&lt;P&gt;and then use something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
where date le &amp;amp;revdate.;
by customer;
if last.customer;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Nov 2020 11:42:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/695904#M212443</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-02T11:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variables that derive their values on a given point on a longitudinal Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/695948#M212470</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;Thanks! This seems to work when I use a small subset of my total dataset (e.g. 150 customers) however when I try to run the code for my entire dataset (roughly 5k customers) the code still hasn't run even though it has been 2 hours!&lt;BR /&gt;&lt;BR /&gt;Have you got any idea why the code would run instantly for 150 customers but seemingly perpetually for 5k customers?&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Mon, 02 Nov 2020 14:06:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/695948#M212470</guid>
      <dc:creator>EC27556</dc:creator>
      <dc:date>2020-11-02T14:06:18Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variables that derive their values on a given point on a longitudinal Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/695958#M212473</link>
      <description>&lt;P&gt;Are the date variables sorted? LIke in my example data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if not, am I allowed to sort them?&lt;/P&gt;</description>
      <pubDate>Mon, 02 Nov 2020 14:19:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/695958#M212473</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-02T14:19:03Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variables that derive their values on a given point on a longitudinal Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/696112#M212556</link>
      <description>&lt;P&gt;Try this. At a little extra memory cost, but should speed things up.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;10958 and 25932 are the numerical representations of the dates 01Jan1990 and 31Dec2030. You can expand as needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id (a1-a3)(:date9.) click1-click3 review :date9.;
format a: review date9.;
datalines;
1 01Jan2020 05Jan2020 10Jan2020 10 20 30 03Jan2020
2 05Jan2020 10Jan2020 15Jan2020 40 50 60 12Jan2020
3 10Jan2020 15Jan2020 20Jan2020 70 80 90 15Jan2020
;

data want (drop=j);
   set have;
   array d {10958 : 25932} _temporary_;
   array a a:;
   array click click:;
   call missing (of d[*]);

   do over a; d[a] = 1; end;
   j = review;

   do while (1);
      j +- 1;
      if d[j] then leave;
   end;

   _I_ = whichn(j, of a[*]);
   v = click;
   
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Nov 2020 07:55:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/696112#M212556</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-03T07:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variables that derive their values on a given point on a longitudinal Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/696120#M212562</link>
      <description>&lt;P&gt;To show you how it works with a proper longitudinal dataset, using&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;'s example data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id (a1-a3)(:date9.) click1-click3 review :date9.;
format a: review yymmdd10.;
datalines;
1 01Jan2020 05Jan2020 10Jan2020 10 20 30 03Jan2020
2 05Jan2020 10Jan2020 15Jan2020 40 50 60 12Jan2020
3 10Jan2020 15Jan2020 20Jan2020 70 80 90 15Jan2020
;

data id;
set have;
keep id review;
run;

proc transpose
  data=have (keep=id a:)
  out=l1 (rename=(col1=a_date))
;
by id;
var a:;
run;

proc transpose
  data=have (keep=id cl:)
  out=l2 (rename=(col1=click))
;
by id;
var cl:;
run;

data l1_a;
set l1;
seq = input(substr(_name_,2),best.);
drop _name_;
run;

data l2_a;
set l2;
seq = input(substr(_name_,6),best.);
drop _name_;
run;

data long;
merge
  l1_a
  l2_a
;
by id seq;
drop seq; /* no longer needed */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You now have two datasets, on containing the "dimension" data of your ID's, the other the "series" data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the structure your data should have right from the start; if you get wide data from any source, always transpose to long as a first step.&lt;/P&gt;
&lt;P&gt;A quicker transpose can be done with this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data long;
set have;
array _date {*} a1-a3;
array _click {*} click1-click3;
do i = 1 to dim(_date);
  a_date = _date{i};
  click = _click{i};
  output;
end;
format a_date yymmdd10.;
keep id a_date click;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With these datasets, you do it in a data step like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
retain _date _click _flag;
merge
  long
  id
;
by id;
if first.id then _flag = 1;
if a_date ge review and _flag
then do;
  a_date = _date;
  click = _click;
  output;
  _flag = 0;
end;
_date = a_date;
_click = click;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Nov 2020 08:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variables-that-derive-their-values-on-a-given-point-on/m-p/696120#M212562</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-03T08:33:42Z</dc:date>
    </item>
  </channel>
</rss>

