<?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: assigning binary values if variable matches in first instance for 'n' times for unsorted data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/assigning-binary-values-if-variable-matches-in-first-instance/m-p/295116#M61635</link>
    <description>&lt;P&gt;Hello RW9,&lt;/P&gt;
&lt;P&gt;Firstly, thank you for your prompt response.&lt;/P&gt;
&lt;P&gt;sorry about the confusion.&lt;/P&gt;
&lt;P&gt;I meant to say table "have" is not sorted. The output table "want" will be sorted.&lt;/P&gt;
&lt;P&gt;I am unable to run the sort operation on the original dataset as the dataset is really huge. A sort operation would take around 3 hours for each dataset - Hence sorting may possibly not be the best solution.&lt;/P&gt;
&lt;P&gt;I have amended the code that i wrote and included your code snippet into my code. Then it works perfectly fine and produces the data.&lt;/P&gt;
&lt;P&gt;However, if i move the data around&amp;nbsp; (i.e. unsort the data i have) then&amp;nbsp;i do not get the want table. Code below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input client $ dates date9. filter $;
datalines ;
Fg5151 31.Jul.06 Y
Fg5151 31.Aug.06 N
Fg5151 28.Feb.06 N
Fg5151 31.Mar.06 N
Fg5151 30.Apr.06 N
Fg5151 31.May.06 Y
Fg5151 30.Jun.06 N
Fg5151 30.Sep.06 N
Fg5151 31.Oct.06 N
Fg5151 30.Nov.06 N
Fg5151 31.Dec.06 N
Fg5151 01.Jan.07 N
A101 30.Apr.06 Y
A101 28.Feb.06 N
A101 31.Mar.06 N
A101 31.May.06 N
A101 30.Jun.06 N
A101 31.Jul.06 N
ABC123 31.Mar.06 N
;


data have;
format dates ddmmyy10.;
set have;
dates=put(dates,best9.);
run;

data _null_;
	set have end=last;
	if _n_ = 1 then do;
		length newdate 8 answer 8 c 8;
		format newdate ddmmyy10.;
		declare hash hs(ordered: "Y", hashexp: 9);
		hs.defineKey("client","dates");
		hs.defineData("client","dates","filter","answer","c");
		hs.defineDone();
	end;
	rc = hs.find();
	if rc ne 0 then do;
		retain answer c;
		if _n_=1 or lag(client) ne client then do;
			answer=0;
			c=0;
		end;
		if filter="Y" then do;
			answer=1;
			c=1;
		end;
		else if answer=1 then c=c+1;
		if c &amp;gt; 6 then do;
			answer=0;
			c=0;
/*			hs.replace();*/
		end;
		hs.replace();
	end;
	if last eq 1 then do;
hs.output(dataset:
		"not_working");
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Table "not_working" is the output of the operation.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table "want" is what i am after:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
input client $ dates date9. filter $ answer $;
datalines ;
A101 28.Feb.06 N 0
A101 31.Mar.06 N 0
A101 30.Apr.06 Y 1
A101 31.May.06 N 1
A101 30.Jun.06 N 1
A101 31.Jul.06 N 1
ABC123 31.Mar.06 N 0
Fg5151 28.Feb.06 N 0
Fg5151 31.Mar.06 N 0
Fg5151 30.Apr.06 N 0
Fg5151 31.May.06 Y 1
Fg5151 30.Jun.06 N 1
Fg5151 31.Jul.06 Y 1
Fg5151 31.Aug.06 N 1
Fg5151 30.Sep.06 N 1
Fg5151 31.Oct.06 N 1
Fg5151 30.Nov.06 N 1
Fg5151 31.Dec.06 N 1
Fg5151 01.Jan.07 N 0
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Many thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Sebastian&lt;/P&gt;</description>
    <pubDate>Tue, 30 Aug 2016 11:57:18 GMT</pubDate>
    <dc:creator>sebster24</dc:creator>
    <dc:date>2016-08-30T11:57:18Z</dc:date>
    <item>
      <title>assigning binary values if variable matches in first instance for 'n' times for unsorted data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assigning-binary-values-if-variable-matches-in-first-instance/m-p/295083#M61625</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I hope you guys are well.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;DATA: &lt;/STRONG&gt;The data is unsorted and hence I am using hash tables. Sorting this table would be a time-consuming effort. If there is no other option, then I will need to sit down for the gruesome sorting approach.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;What&amp;nbsp;I want:&lt;/STRONG&gt; I am trying to enumerate a table variable "answer"&amp;nbsp;with binary values (0/1) if variable filter = "Y" for the next 6 observations with the same client. In short if variable filter "Y" then this observation and the next&amp;nbsp;5 observations for &lt;STRONG&gt;&lt;U&gt;same client&lt;/U&gt;&lt;/STRONG&gt; should be assigned variable "answer" eq 1, else 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have presented the "have" and "want" data below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input client $ dates date9. filter $;
datalines ;
Fg5151 28.Feb.06 N
Fg5151 31.Mar.06 N
Fg5151 30.Apr.06 N
Fg5151 31.May.06 Y
Fg5151 30.Jun.06 N
Fg5151 31.Jul.06 Y
Fg5151 31.Aug.06 N
Fg5151 30.Sep.06 N
Fg5151 31.Oct.06 N
Fg5151 30.Nov.06 N
Fg5151 31.Dec.06 N
Fg5151 01.Jan.07 N
A101 28.Feb.06 N
A101 31.Mar.06 N
A101 30.Apr.06 Y
A101 31.May.06 N
A101 30.Jun.06 N
A101 31.Jul.06 N
ABC123 31.Mar.06 N
;


data want;
input client $ dates date9. filter $ answer 8;
datalines ;
A101 28.Feb.06 N 0
A101 31.Mar.06 N 0
A101 30.Apr.06 Y 1
A101 31.May.06 N 1
A101 30.Jun.06 N 1
A101 31.Jul.06 N 1
ABC123 31.Mar.06 N 0
Fg5151 28.Feb.06 N 0
Fg5151 31.Mar.06 N 0
Fg5151 30.Apr.06 N 0
Fg5151 31.May.06 Y 1
Fg5151 30.Jun.06 N 1
Fg5151 31.Jul.06 Y 1
Fg5151 31.Aug.06 N 1
Fg5151 30.Sep.06 N 1
Fg5151 31.Oct.06 N 1
Fg5151 30.Nov.06 N 1
Fg5151 31.Dec.06 N 1
Fg5151 01.Jan.07 N 0
;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have compiled the HASH code below - The code works, but variable "answer" provides unsatisfactory results:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	set have end=last;
	retain counter;
	if _n_ = 1 then do;
		length newdate 8 answer 8;
		format newdate ddmmyy10.;
		declare hash hs(ordered: "Y", hashexp: 9);
		hs.defineKey("client","dates");
		hs.defineData("client","dates","filter","answer","newdate");
		hs.defineDone();
	end;
	rc = hs.find();
	if rc ne 0 then do;
		if filter eq "Y" then do;
			counter+0;
			by CLIENT filter notsorted;
			if first.filter then do;
				answer=1;
				newdate=intnx('month',dates,6,"same");
			end;
		end;
		hs.add();
	end;
	else do;
		counter=0;
	end;
	if last eq 1 then do;
	hs.output(dataset: "want");
	end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Aug 2016 08:53:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assigning-binary-values-if-variable-matches-in-first-instance/m-p/295083#M61625</guid>
      <dc:creator>sebster24</dc:creator>
      <dc:date>2016-08-30T08:53:23Z</dc:date>
    </item>
    <item>
      <title>Re: assigning binary values if variable matches in first instance for 'n' times for unsorted data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assigning-binary-values-if-variable-matches-in-first-instance/m-p/295091#M61626</link>
      <description>&lt;P&gt;Your want dataset does not match the request - you state you do not want to sort the data, but want is sorted?&lt;/P&gt;
&lt;P&gt;You could do it using retain:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain answer c;
  if _n_=1 or lag(client) ne client then do;
    answer=0;
    c=0;
  end;
  if filter="Y" then do;
    answer=1;
    c=1;
  end;
  else if answer=1 then c=c+1;
  if c &amp;gt; 6 then do;
    answer=0;
    c=0;
  end;
run;

&lt;/PRE&gt;
&lt;P&gt;However I would really advise sorting the data as unlike databases, SAS works better on sorted data (i.e. by group processing).&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2016 09:43:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assigning-binary-values-if-variable-matches-in-first-instance/m-p/295091#M61626</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-08-30T09:43:06Z</dc:date>
    </item>
    <item>
      <title>Re: assigning binary values if variable matches in first instance for 'n' times for unsorted data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assigning-binary-values-if-variable-matches-in-first-instance/m-p/295116#M61635</link>
      <description>&lt;P&gt;Hello RW9,&lt;/P&gt;
&lt;P&gt;Firstly, thank you for your prompt response.&lt;/P&gt;
&lt;P&gt;sorry about the confusion.&lt;/P&gt;
&lt;P&gt;I meant to say table "have" is not sorted. The output table "want" will be sorted.&lt;/P&gt;
&lt;P&gt;I am unable to run the sort operation on the original dataset as the dataset is really huge. A sort operation would take around 3 hours for each dataset - Hence sorting may possibly not be the best solution.&lt;/P&gt;
&lt;P&gt;I have amended the code that i wrote and included your code snippet into my code. Then it works perfectly fine and produces the data.&lt;/P&gt;
&lt;P&gt;However, if i move the data around&amp;nbsp; (i.e. unsort the data i have) then&amp;nbsp;i do not get the want table. Code below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input client $ dates date9. filter $;
datalines ;
Fg5151 31.Jul.06 Y
Fg5151 31.Aug.06 N
Fg5151 28.Feb.06 N
Fg5151 31.Mar.06 N
Fg5151 30.Apr.06 N
Fg5151 31.May.06 Y
Fg5151 30.Jun.06 N
Fg5151 30.Sep.06 N
Fg5151 31.Oct.06 N
Fg5151 30.Nov.06 N
Fg5151 31.Dec.06 N
Fg5151 01.Jan.07 N
A101 30.Apr.06 Y
A101 28.Feb.06 N
A101 31.Mar.06 N
A101 31.May.06 N
A101 30.Jun.06 N
A101 31.Jul.06 N
ABC123 31.Mar.06 N
;


data have;
format dates ddmmyy10.;
set have;
dates=put(dates,best9.);
run;

data _null_;
	set have end=last;
	if _n_ = 1 then do;
		length newdate 8 answer 8 c 8;
		format newdate ddmmyy10.;
		declare hash hs(ordered: "Y", hashexp: 9);
		hs.defineKey("client","dates");
		hs.defineData("client","dates","filter","answer","c");
		hs.defineDone();
	end;
	rc = hs.find();
	if rc ne 0 then do;
		retain answer c;
		if _n_=1 or lag(client) ne client then do;
			answer=0;
			c=0;
		end;
		if filter="Y" then do;
			answer=1;
			c=1;
		end;
		else if answer=1 then c=c+1;
		if c &amp;gt; 6 then do;
			answer=0;
			c=0;
/*			hs.replace();*/
		end;
		hs.replace();
	end;
	if last eq 1 then do;
hs.output(dataset:
		"not_working");
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Table "not_working" is the output of the operation.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table "want" is what i am after:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
input client $ dates date9. filter $ answer $;
datalines ;
A101 28.Feb.06 N 0
A101 31.Mar.06 N 0
A101 30.Apr.06 Y 1
A101 31.May.06 N 1
A101 30.Jun.06 N 1
A101 31.Jul.06 N 1
ABC123 31.Mar.06 N 0
Fg5151 28.Feb.06 N 0
Fg5151 31.Mar.06 N 0
Fg5151 30.Apr.06 N 0
Fg5151 31.May.06 Y 1
Fg5151 30.Jun.06 N 1
Fg5151 31.Jul.06 Y 1
Fg5151 31.Aug.06 N 1
Fg5151 30.Sep.06 N 1
Fg5151 31.Oct.06 N 1
Fg5151 30.Nov.06 N 1
Fg5151 31.Dec.06 N 1
Fg5151 01.Jan.07 N 0
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Many thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Sebastian&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2016 11:57:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assigning-binary-values-if-variable-matches-in-first-instance/m-p/295116#M61635</guid>
      <dc:creator>sebster24</dc:creator>
      <dc:date>2016-08-30T11:57:18Z</dc:date>
    </item>
    <item>
      <title>Re: assigning binary values if variable matches in first instance for 'n' times for unsorted data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assigning-binary-values-if-variable-matches-in-first-instance/m-p/295165#M61655</link>
      <description>&lt;PRE&gt;

data have;
input client $ dates : date9. filter $;
n+1;
format dates date9.;
datalines ;
Fg5151 28.Feb.06 N
Fg5151 31.Mar.06 N
Fg5151 30.Apr.06 N
Fg5151 31.May.06 Y
Fg5151 30.Jun.06 N
Fg5151 31.Jul.06 Y
Fg5151 31.Aug.06 N
Fg5151 30.Sep.06 N
Fg5151 31.Oct.06 N
Fg5151 30.Nov.06 N
Fg5151 31.Dec.06 N
Fg5151 01.Jan.07 N
A101 28.Feb.06 N
A101 31.Mar.06 N
A101 30.Apr.06 Y
A101 31.May.06 N
A101 30.Jun.06 N
A101 31.Jul.06 N
ABC123 31.Mar.06 N
;
run;
data key;
 set have(keep=client filter n where=(filter='Y'));
 output;
 do i=1 to 5;
  n=n+1;output;
 end;
 drop i;
run;
data want;
 if _n_=1 then do;
  declare hash h(dataset:'key',hashexp:20);
  h.definekey('client','n');
  h.definedone();
 end;
set have;
if h.check()=0 then answer=1;
 else answer=0;
run;

&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Aug 2016 14:01:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assigning-binary-values-if-variable-matches-in-first-instance/m-p/295165#M61655</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-30T14:01:37Z</dc:date>
    </item>
    <item>
      <title>Re: assigning binary values if variable matches in first instance for 'n' times for unsorted data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assigning-binary-values-if-variable-matches-in-first-instance/m-p/295172#M61660</link>
      <description>Hello Xia Keshan (KSharp),&lt;BR /&gt;Instead of picking up next 6 observations, what if I want observations in the next 6 months?&lt;BR /&gt;I created a post: &lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/sas-enumeration-variable-for-next-6-months/m-p/295164" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/sas-enumeration-variable-for-next-6-months/m-p/295164&lt;/A&gt;&lt;BR /&gt;thank you so much.</description>
      <pubDate>Tue, 30 Aug 2016 14:13:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assigning-binary-values-if-variable-matches-in-first-instance/m-p/295172#M61660</guid>
      <dc:creator>sebster24</dc:creator>
      <dc:date>2016-08-30T14:13:04Z</dc:date>
    </item>
  </channel>
</rss>

