<?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: Create a Dummy Variable based on values in multiple different rows in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/537017#M6721</link>
    <description>&lt;P&gt;I tried adding a counter to make it work in both cases, instead it stopped working in either one.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  array possible_days {1:1000} _temporary_  (1000*0);
  events=0;
  do until (end_of_have);
    set have end=end_of_have;
    if Thing=1 then do;
    	events+1;
    	d=max(day-30, 1) to day;
    	possible_days{d}=1;
    end;
  end;
  if events=0 then do;
  	d=day-29 to 1000;
    possible_days{d}=.;
  end;
  drop d;
  drop events;
  do until (end_of_have);
    set have end=end_of_have;
    early_warning=possible_days{day};
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;However the code seems like it should work? (more likely I'm just clueless I guess)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The log locates the errors in the&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;d=max(day-30, 1) to day;

d=day-29 to 1000;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;lines saying 'syntax error' and 'expecting arithmetic operator' respectively.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 20 Feb 2019 09:41:56 GMT</pubDate>
    <dc:creator>dcd</dc:creator>
    <dc:date>2019-02-20T09:41:56Z</dc:date>
    <item>
      <title>Create a Dummy Variable based on values in multiple different rows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536832#M6671</link>
      <description>&lt;DIV class="sasSource"&gt;Hello, I'd very much appreciate a certain amount of help with this.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;This is my current data:&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;data WORK.HAVE;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;infile datalines dsd truncover;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;input Thing:32. Day:32.;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;datalines;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 4&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 11&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 14&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 19&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 28&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 35&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 41&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;1 48&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;1 55&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;;;;;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;I want to create an Early_Warning dummy Variable that will take the value of 1, if Thing is happening with the next 30 days.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;So in this case this is what the result should look like:&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&lt;DIV class="sasSource"&gt;data WORK.WANT;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;infile datalines dsd truncover;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;input Thing:32. Day:32. Early_Warning:32.;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;datalines;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 1 0&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 4 0&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 11 0&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 14 0&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 19 1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 28 1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 35 1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 41 1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;1 48 1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;1 55 1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;;;;;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;Lastly, if we can't be sure whether Thing is happening within the next 30 days, I want the Early_Warning to be null and the data should look like this.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&lt;DIV class="sasSource"&gt;data WORK.WANT2;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;infile datalines dsd truncover;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;input Thing:32. Day:32. Early_Warning:32.;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;datalines;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 1 0&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 4 0&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 11 0&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 14 0&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 19 0&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 28 .&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 35 .&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 41 .&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 48 .&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;0 55 .&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;;;;;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 19 Feb 2019 17:28:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536832#M6671</guid>
      <dc:creator>dcd</dc:creator>
      <dc:date>2019-02-19T17:28:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Dummy Variable based on values in multiple different rows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536838#M6674</link>
      <description>&lt;P&gt;Let's say you know&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Data are sorted by DAY&lt;/LI&gt;
&lt;LI&gt;DAY is always a positive integer that will never exceed 1000.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Then you could:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Initialize a 1,000-element array with zeroes.&lt;/LI&gt;
&lt;LI&gt;Read in all the records with thing=1, and set to 1 all the the array elements corresponding to DAY-1 through DAY.&lt;/LI&gt;
&lt;LI&gt;After reading in all the thing=1 records, set all the elements from last DAY + 1 to 1000 as missing values.&lt;/LI&gt;
&lt;LI&gt;Reread the&amp;nbsp; entire HAVE dataset and pull a value from the array that corresponds to DAY.&lt;/LI&gt;
&lt;/OL&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;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.HAVE;
  infile datalines truncover;
  input Thing  Day ;
datalines;
0 1
0 4
0 11
0 14
0 19
0 28
0 35
0 41
1 48
1 55
;;;;

data want;
  array possible_days {1:1000} _temporary_  (1000*0);
  do until (end_of_thing_eq_1);
    set have (where=(thing=1)) end=end_of_thing_eq_1;
    do d=day-30 to day;
       possible_days{d}=1;
    end;
  end;
  do d=day+1 to 1000;
    possible_days{d}=.;
  end;
  drop d;
  do until (end_of_have);
    set have end=end_of_have;
    early_warning=possible_days{day};
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Feb 2019 17:49:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536838#M6674</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-02-19T17:49:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Dummy Variable based on values in multiple different rows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536851#M6676</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/261206"&gt;@dcd&lt;/a&gt;&amp;nbsp; &amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;'s solution is full proof.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I played mine as I was bored.&lt;/P&gt;
&lt;P&gt;Anyways,&amp;nbsp; for want2 ,&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;if we can't be sure whether Thing is happening within the next 30 days,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;then how come earlywarning is zero upto&amp;nbsp;&lt;STRONG&gt;0 19 0 &lt;/STRONG&gt;assuming all records of thing is 0 as your want2 indicates. What and where am i missing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="sasSource"&gt;0 1 0&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;0 4 0&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;0 11 0&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;0 14 0&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;0 19 0&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data WORK.HAVE;
  infile datalines truncover;
  input Thing  Day ;
datalines;
0 1
0 4
0 11
0 14
0 19
0 28
0 35
0 41
1 48
1 55
;;;;


proc sql;
create table want as
select distinct a.*,(a.day&amp;lt;=b.day&amp;lt;=a.day+30) as &lt;SPAN&gt;Early_Warning&lt;/SPAN&gt;
from have a left join have(where=(thing=1)) b
on a.day&amp;lt;=b.day&amp;lt;=a.day+30
order by thing,a.day;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;</description>
      <pubDate>Tue, 19 Feb 2019 18:50:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536851#M6676</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-19T18:50:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Dummy Variable based on values in multiple different rows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536875#M6681</link>
      <description>&lt;P&gt;In want2 early_warning is 0 up to day 19 because we do have info up to day 55 -that is more than 30 days later- that there is no triggering event. In day 28 we don't have info for 30 days -day 55 is only 27 days away- so we can't be sure, hence I'd like a null there.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Feb 2019 19:37:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536875#M6681</guid>
      <dc:creator>dcd</dc:creator>
      <dc:date>2019-02-19T19:37:25Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Dummy Variable based on values in multiple different rows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536879#M6683</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/261206"&gt;@dcd&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Are you aware that my program is intended to produce results satisfying BOTH of your objectives?&lt;/P&gt;</description>
      <pubDate>Tue, 19 Feb 2019 19:44:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536879#M6683</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-02-19T19:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Dummy Variable based on values in multiple different rows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536904#M6686</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/261206"&gt;@dcd&lt;/a&gt;&amp;nbsp; Thank you for clarifying. I am afraid while proc sql syntax can be made to look simple, that condition might warrant another pass or a Cartesian for that matter which would make it inefficient in my opinion. Well I am pleased and glad you have got a datastep solution though&lt;/P&gt;</description>
      <pubDate>Tue, 19 Feb 2019 20:32:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536904#M6686</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-19T20:32:08Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Dummy Variable based on values in multiple different rows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536911#M6687</link>
      <description>&lt;P&gt;Here is an illustration of what I mean in my previous message:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data WORK.HAVE;
  infile datalines truncover;
  input Thing  Day ;
datalines;
0 1
0 4
0 11
0 14
0 19
0 28
0 35
0 41
1 48
1 55
;;;;
/*Test with thing positive 1*/
proc sql;
create table want(drop=d) as
select distinct a.*,ifn((a.day&amp;lt;=b.day&amp;lt;=a.day+30)=0,d,a.day&amp;lt;=b.day&amp;lt;=a.day+30) as Early_Warning
from (select *,ifn(max(day)&amp;gt;day+30,0,.) as d from have) a left join have(where=(thing=1)) b
on a.day&amp;lt;=b.day&amp;lt;=a.day+30
order by thing,a.day;
quit;
/*Test with all Thing 0*/
data WORK.HAVE;
  infile datalines truncover;
  input Thing  Day ;
datalines;
0 1
0 4
0 11
0 14
0 19
0 28
0 35
0 41
0 48
0 55
;;;;
proc sql;
create table want(drop=d) as
select distinct a.*,ifn((a.day&amp;lt;=b.day&amp;lt;=a.day+30)=0,d,a.day&amp;lt;=b.day&amp;lt;=a.day+30) as Early_Warning
from (select *,ifn(max(day)&amp;gt;day+30,0,.) as d from have) a left join have(where=(thing=1)) b
on a.day&amp;lt;=b.day&amp;lt;=a.day+30
order by thing,a.day;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Feb 2019 21:09:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536911#M6687</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-19T21:09:18Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Dummy Variable based on values in multiple different rows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536990#M6711</link>
      <description>&lt;P&gt;I was just clarifying to novinosrin. I only now got the chance to try what you suggested but I'm getting an empty table when the Thing is always 0. Any idea why is that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also there is an out of subscript error when Thing happens on say day 10 but that's easy to fix.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 08:37:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/536990#M6711</guid>
      <dc:creator>dcd</dc:creator>
      <dc:date>2019-02-20T08:37:05Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Dummy Variable based on values in multiple different rows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/537017#M6721</link>
      <description>&lt;P&gt;I tried adding a counter to make it work in both cases, instead it stopped working in either one.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  array possible_days {1:1000} _temporary_  (1000*0);
  events=0;
  do until (end_of_have);
    set have end=end_of_have;
    if Thing=1 then do;
    	events+1;
    	d=max(day-30, 1) to day;
    	possible_days{d}=1;
    end;
  end;
  if events=0 then do;
  	d=day-29 to 1000;
    possible_days{d}=.;
  end;
  drop d;
  drop events;
  do until (end_of_have);
    set have end=end_of_have;
    early_warning=possible_days{day};
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;However the code seems like it should work? (more likely I'm just clueless I guess)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The log locates the errors in the&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;d=max(day-30, 1) to day;

d=day-29 to 1000;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;lines saying 'syntax error' and 'expecting arithmetic operator' respectively.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 09:41:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/537017#M6721</guid>
      <dc:creator>dcd</dc:creator>
      <dc:date>2019-02-20T09:41:56Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Dummy Variable based on values in multiple different rows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/537030#M6722</link>
      <description>&lt;P&gt;This works perfectly, thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 10:37:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-Dummy-Variable-based-on-values-in-multiple-different/m-p/537030#M6722</guid>
      <dc:creator>dcd</dc:creator>
      <dc:date>2019-02-20T10:37:00Z</dc:date>
    </item>
  </channel>
</rss>

