<?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: Select n rows before and after an observation based on 2 criteria in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/543470#M150233</link>
    <description>&lt;P&gt;another way to obtain your result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data _null_;
   set have;
   by id ;
   if first.id then n=1;
   else n+1;
   if missing(anncmt)=0 then call symputx(compress('id'||id),n);
run;

data want(drop=n);
   set have;
   by id;
   if first.id then n=1;
   else n+1;
   if symget(compress('id'||id))-3&amp;lt;=n&amp;lt;=symget(compress('id'||id))+3 then output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;best regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sébastien&lt;/P&gt;</description>
    <pubDate>Fri, 15 Mar 2019 10:23:45 GMT</pubDate>
    <dc:creator>SR_FR</dc:creator>
    <dc:date>2019-03-15T10:23:45Z</dc:date>
    <item>
      <title>Select n rows before and after an observation based on 2 criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/543450#M150226</link>
      <description>&lt;P&gt;Good evening SAS community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a question on how I can modify my code for the following problem:&lt;/P&gt;&lt;P&gt;My data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input ID Date ret anncmt;&lt;BR /&gt;datalines;&lt;BR /&gt;1 19990205 0.01 .&lt;BR /&gt;1 19990206 0.02 .&lt;BR /&gt;1 19990207 0.04 .&lt;BR /&gt;1 19990208 0.01 .&lt;BR /&gt;1 19990209 0.02 19990209&lt;BR /&gt;1 19990210 0.01 .&lt;BR /&gt;1 19990211 0.02 .&lt;BR /&gt;1 19990212 0.03 .&lt;BR /&gt;2 19990205 0.01 .&lt;BR /&gt;2 19990206 0.02 .&lt;BR /&gt;2 19990207 0.04 .&lt;BR /&gt;2 19990208 0.01 .&lt;BR /&gt;2 19990209 0.02 19990209&lt;BR /&gt;2 19990210 0.01 .&lt;BR /&gt;2 19990211 0.02 .&lt;BR /&gt;3 19990205 0.01 .&lt;BR /&gt;3 19990206 0.02 .&lt;BR /&gt;3 19990207 0.04 .&lt;BR /&gt;3 19990208 0.01 .&lt;BR /&gt;3 19990209 0.02 19990209&lt;BR /&gt;3 19990210 0.01 .&lt;BR /&gt;3 19990211 0.02 .&lt;BR /&gt;3 19990212 0.03 .&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to happen now is that SAS creates a new dataset selecting only the 3 observations before and after the anncmt date:&lt;/P&gt;&lt;P&gt;In the moment I'm using the following code which I copied from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt; (&lt;A href="https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation/m-p/503029/highlight/false#M134357" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation/m-p/503029/highlight/false#M134357&lt;/A&gt;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; anncmt&lt;SPAN class="token operator"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; do p&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;max&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;_n_&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; to &lt;SPAN class="token function"&gt;min&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;_n_&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;nobs&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; have &lt;SPAN class="token function"&gt;point&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;p nobs&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;nobs&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is that the code does not account for the ID. I only want to have the +3/-3 observations of the same ID. The Output of the above code will be like this:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;1 19990206 0.02 .&lt;BR /&gt;1 19990207 0.04 .&lt;BR /&gt;1 19990208 0.01 .&lt;BR /&gt;1 19990209 0.02 19990209&lt;BR /&gt;1 19990210 0.01 .&lt;BR /&gt;1 19990211 0.02 .&lt;BR /&gt;1 19990212 0.03 .&lt;BR /&gt;2 19990206 0.02 .&lt;BR /&gt;2 19990207 0.04 .&lt;BR /&gt;2 19990208 0.01 .&lt;BR /&gt;2 19990209 0.02 19990209&lt;BR /&gt;2 19990210 0.01 .&lt;BR /&gt;2 19990211 0.02 .&lt;BR /&gt;3 19990205 0.01 .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (Here is the problem!!)&lt;/P&gt;&lt;P&gt;3 19990206 0.02 .&lt;BR /&gt;3 19990207 0.04 .&lt;BR /&gt;3 19990208 0.01 .&lt;BR /&gt;3 19990209 0.02 19990209&lt;BR /&gt;3 19990210 0.01 .&lt;BR /&gt;3 19990211 0.02 .&lt;BR /&gt;3 19990212 0.03 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I would like to happen is that SAS only selects the +3/-3 observations for the same ID and if there aren't enough observations to fulfill this (like in the data we have in this example) I want SAS to stop when a new ID starts. So the Output should look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;1 19990206 0.02 .&lt;BR /&gt;1 19990207 0.04 .&lt;BR /&gt;1 19990208 0.01 .&lt;BR /&gt;1 19990209 0.02 19990209&lt;BR /&gt;1 19990210 0.01 .&lt;BR /&gt;1 19990211 0.02 .&lt;BR /&gt;1 19990212 0.03 .&lt;BR /&gt;2 19990206 0.02 .&lt;BR /&gt;2 19990207 0.04 .&lt;BR /&gt;2 19990208 0.01 .&lt;BR /&gt;2 19990209 0.02 19990209&lt;BR /&gt;2 19990210 0.01 .&lt;BR /&gt;2 19990211 0.02 .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (SAS stops with this obs even though it is only +2 because ID changes in next obs)&lt;/P&gt;&lt;P&gt;3 19990206 0.02 .&lt;BR /&gt;3 19990207 0.04 .&lt;BR /&gt;3 19990208 0.01 .&lt;BR /&gt;3 19990209 0.02 19990209&lt;BR /&gt;3 19990210 0.01 .&lt;BR /&gt;3 19990211 0.02 .&lt;BR /&gt;3 19990212 0.03 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What would be amazing also is if in any way SAS would report when there are not enough obs. for a respective ID to fulfill the +3/-3 and only +2/-3 has been selected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hopefully I described my problem sufficiently.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks to everyone in advance for help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please bear with me, I'm pretty new to SAS and thus will probably not understand complex code &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt; Therefore an "easy" solution would be appreciated or some short explanations on the code so I at least know what exactly is happening and do not just copy the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again and Best regards&lt;/P&gt;&lt;P&gt;Nici&lt;/P&gt;</description>
      <pubDate>Fri, 15 Mar 2019 09:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/543450#M150226</guid>
      <dc:creator>Nici</dc:creator>
      <dc:date>2019-03-15T09:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Select n rows before and after an observation based on 2 criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/543456#M150229</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/146551"&gt;@Nici&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;I hope this works for you:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=x y);
	set have;
	x = id;

	if anncmt&amp;gt;. then
		do p=max(_n_-3,1) to min(_n_+3,nobs);
			set have point=p nobs=nobs;

			if x=id then
				output;
			else
				do;
					y = cat("Missing obs for the id: ",x);
					put y;
				end;
		end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get the output:&lt;/P&gt;
&lt;PRE&gt;1	19990206	0.02	
1	19990207	0.04	
1	19990208	0.01	
1	19990209	0.02	19990209
1	19990210	0.01	
1	19990211	0.02	
1	19990212	0.03	
2	19990206	0.02	
2	19990207	0.04	
2	19990208	0.01	
2	19990209	0.02	19990209
2	19990210	0.01	
2	19990211	0.02	
3	19990206	0.02	
3	19990207	0.04	
3	19990208	0.01	
3	19990209	0.02	19990209
3	19990210	0.01	
3	19990211	0.02	
3	19990212	0.03	&lt;/PRE&gt;
&lt;P&gt;In the log one&amp;nbsp;Missing obs for the id: 2 is added you could also output these to another table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have any question about the coding please let me know.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards&lt;/P&gt;
&lt;P&gt;Criptic&lt;/P&gt;</description>
      <pubDate>Fri, 15 Mar 2019 09:59:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/543456#M150229</guid>
      <dc:creator>Criptic</dc:creator>
      <dc:date>2019-03-15T09:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: Select n rows before and after an observation based on 2 criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/543470#M150233</link>
      <description>&lt;P&gt;another way to obtain your result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data _null_;
   set have;
   by id ;
   if first.id then n=1;
   else n+1;
   if missing(anncmt)=0 then call symputx(compress('id'||id),n);
run;

data want(drop=n);
   set have;
   by id;
   if first.id then n=1;
   else n+1;
   if symget(compress('id'||id))-3&amp;lt;=n&amp;lt;=symget(compress('id'||id))+3 then output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;best regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sébastien&lt;/P&gt;</description>
      <pubDate>Fri, 15 Mar 2019 10:23:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/543470#M150233</guid>
      <dc:creator>SR_FR</dc:creator>
      <dc:date>2019-03-15T10:23:45Z</dc:date>
    </item>
    <item>
      <title>Re: Select n rows before and after an observation based on 2 criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/543598#M150297</link>
      <description>&lt;P&gt;Your code works perfectly and is super intuitive.&lt;/P&gt;&lt;P&gt;Thank you very much! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BR&lt;/P&gt;</description>
      <pubDate>Fri, 15 Mar 2019 17:32:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/543598#M150297</guid>
      <dc:creator>Nici</dc:creator>
      <dc:date>2019-03-15T17:32:47Z</dc:date>
    </item>
    <item>
      <title>Re: Select n rows before and after an observation based on 2 criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/543600#M150298</link>
      <description>&lt;P&gt;Thanks a lot for taking the time to look at my problem and coming up with a solution. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tested your code and it works perfectly as well. Unfortunately I cannot accept two posts as the solution for this problem. &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BR&lt;/P&gt;&lt;P&gt;Nici&lt;/P&gt;</description>
      <pubDate>Fri, 15 Mar 2019 17:36:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/543600#M150298</guid>
      <dc:creator>Nici</dc:creator>
      <dc:date>2019-03-15T17:36:04Z</dc:date>
    </item>
    <item>
      <title>Re: Select n rows before and after an observation based on 2 criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/544300#M150515</link>
      <description>&lt;P&gt;Hey&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/50279"&gt;@Criptic&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;something changed in my data and now the code you provided does not work anymore &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data looks like this now:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data cfma.parentstockdata;&lt;BR /&gt;input ID Date ret anncmt flag;&lt;BR /&gt;datalines;&lt;BR /&gt;1 19990205 0.01 19990209 0&lt;BR /&gt;1 19990206 0.02 19990209 0&lt;BR /&gt;1 19990207 0.04 19990209 0&lt;BR /&gt;1 19990208 0.01 19990209 0&lt;BR /&gt;1 19990209 0.02 19990209 1&lt;BR /&gt;1 19990210 0.01 19990209 0&lt;BR /&gt;1 19990211 0.02 19990209 0&lt;BR /&gt;1 19990212 0.03 19990209 0&lt;BR /&gt;2 19990205 0.01 19990209 0&lt;BR /&gt;2 19990206 0.02 19990209 0&lt;BR /&gt;2 19990207 0.04 19990209 0&lt;BR /&gt;2 19990208 0.01 19990209 0&lt;BR /&gt;2 19990209 0.02 19990209 1&lt;BR /&gt;2 19990210 0.01 19990209 0&lt;BR /&gt;2 19990211 0.02 19990209 0&lt;BR /&gt;3 19990205 0.01 19990209 0&lt;BR /&gt;3 19990206 0.02 19990209 0&lt;BR /&gt;3 19990207 0.04 19990209 0&lt;BR /&gt;3 19990208 0.01 19990209 0&lt;BR /&gt;3 19990209 0.02 19990209 1&lt;BR /&gt;3 19990210 0.01 19990209 0&lt;BR /&gt;3 19990211 0.02 19990209 0&lt;BR /&gt;3 19990212 0.03 19990209 0&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I adjusted your code a little bit and it now looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data CRhelp5(drop=x y);&lt;BR /&gt;set cfma.parentstockdata;&lt;BR /&gt;x = ID;&lt;/P&gt;&lt;P&gt;if flag=1 then&lt;BR /&gt;do p=max(_n_-5,1) to min(_n_+5,nobs);&lt;BR /&gt;set cfma.parentstockdata point=p nobs=nobs;&lt;/P&gt;&lt;P&gt;if x=ID then&lt;BR /&gt;output;&lt;BR /&gt;else&lt;BR /&gt;do;&lt;BR /&gt;y = cat("Missing obs for the pcusip: ",x);&lt;BR /&gt;put y;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not really get why it is not working anymore since the only thing that changed in my code is basically the new variable flag. Somehow I do not get an output anymore though. CRHelp5 has 0 observations and my whole log is full of the message "missing obs for the ID ...."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have an idea what could be the problem?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much in advance and have a great evening &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BR&lt;/P&gt;&lt;P&gt;Nici&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2019 18:04:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/544300#M150515</guid>
      <dc:creator>Nici</dc:creator>
      <dc:date>2019-03-19T18:04:39Z</dc:date>
    </item>
    <item>
      <title>Re: Select n rows before and after an observation based on 2 criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/544442#M150567</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/146551"&gt;@Nici&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;does your log contain any Error or Warning Messages? Because the code runs fine for me. All rows are retained because of the increase from 3 to 5 but that looks to be correct and I get the following messages in the log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Missing obs for the pcusip: 1
Missing obs for the pcusip: 1
Missing obs for the pcusip: 2
Missing obs for the pcusip: 2
Missing obs for the pcusip: 2
Missing obs for the pcusip: 2
Missing obs for the pcusip: 3
NOTE: There were 23 observations read from the data set WORK.PARENTSTOCKDATA.
NOTE: The data set WORK.CRHELP5 has 23 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Mar 2019 05:37:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/544442#M150567</guid>
      <dc:creator>Criptic</dc:creator>
      <dc:date>2019-03-20T05:37:47Z</dc:date>
    </item>
    <item>
      <title>Re: Select n rows before and after an observation based on 2 criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/544483#M150587</link>
      <description>&lt;P&gt;Hey &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/50279"&gt;@Criptic&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you for your answer. I've found the source of the problem in my dataset. The problem is that the variable 'ID' is not a numeric but a character variable. ID's like "54140Y" are possible. How can I adjust the code so it works for character variables as well?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get the error:&lt;/P&gt;&lt;P&gt;"NOTE: Invalid numeric data, ID='54140Y'...."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much and have a great day.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BR&lt;/P&gt;&lt;P&gt;Nici&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2019 09:25:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/544483#M150587</guid>
      <dc:creator>Nici</dc:creator>
      <dc:date>2019-03-20T09:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: Select n rows before and after an observation based on 2 criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/544493#M150590</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/146551"&gt;@Nici&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to replicate your error but couldn't. The code works fine for me even if the variable is character. The variable x (which holds the id) takes on the same format as the ID column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My example code / modified data to replicate different length and character IDs:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.parentstockdata;
length ID $2.;
input ID Date ret anncmt flag;
datalines;
12 19990205 0.01 19990209 0
1 19990206 0.02 19990209 0
1F 19990207 0.04 19990209 0
1 19990208 0.01 19990209 0
1F 19990209 0.02 19990209 1
1 19990210 0.01 19990209 0
1 19990211 0.02 19990209 0
1 19990212 0.03 19990209 0
2 19990205 0.01 19990209 0
2 19990206 0.02 19990209 0
2 19990207 0.04 19990209 0
2 19990208 0.01 19990209 0
2 19990209 0.02 19990209 1
2 19990210 0.01 19990209 0
2 19990211 0.02 19990209 0
3 19990205 0.01 19990209 0
3 19990206 0.02 19990209 0
3 19990207 0.04 19990209 0
3 19990208 0.01 19990209 0
3 19990209 0.02 19990209 1
3 19990210 0.01 19990209 0
3 19990211 0.02 19990209 0
3 19990212 0.03 19990209 0
;
run;

data CRhelp5(drop=y);
	set work.parentstockdata;
	x = ID;

	if flag=1 then
		do p=max(_n_-5,1) to min(_n_+5,nobs);
			set work.parentstockdata point=p nobs=nobs;

			if x=ID then
				output;
			else
				do;
					y = cat("Missing obs for the pcusip: ",x);
					put y;
				end;
		end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the log I get the following messages:&lt;/P&gt;
&lt;PRE&gt;Missing obs for the pcusip: 1F
Missing obs for the pcusip: 1F
Missing obs for the pcusip: 1F
Missing obs for the pcusip: 1F
Missing obs for the pcusip: 1F
Missing obs for the pcusip: 1F
Missing obs for the pcusip: 1F
Missing obs for the pcusip: 1F
Missing obs for the pcusip: 2
Missing obs for the pcusip: 2
Missing obs for the pcusip: 2
Missing obs for the pcusip: 2
Missing obs for the pcusip: 3
NOTE: There were 23 observations read from the data set WORK.PARENTSTOCKDATA.
NOTE: The data set WORK.CRHELP5 has 17 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.00 seconds&lt;/PRE&gt;
&lt;P&gt;Please check that your ID variable in the dataset parentstockdata is defined as a alphanumerical field.&lt;/P&gt;
&lt;P&gt;If this doesn't help you please try to replicate your error with example data and post it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards&lt;/P&gt;
&lt;P&gt;Criptic&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2019 09:48:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/544493#M150590</guid>
      <dc:creator>Criptic</dc:creator>
      <dc:date>2019-03-20T09:48:12Z</dc:date>
    </item>
    <item>
      <title>Re: Select n rows before and after an observation based on 2 criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/544499#M150593</link>
      <description>&lt;P&gt;Hey &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/50279"&gt;@Criptic&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I finally got it to work. The problem was totally my fault and I'm super sorry for wasting some of your time. In my dataset I actually already had help variables called "x" and "y". I did not mention them here because as I said they are just help variables. Obviously SAS has troubles if the same variables are used twice...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Again, I'm very sorry!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your on-going support throughout my struggles.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have an amazing day.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BR&lt;/P&gt;&lt;P&gt;Nici&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2019 10:35:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/544499#M150593</guid>
      <dc:creator>Nici</dc:creator>
      <dc:date>2019-03-20T10:35:35Z</dc:date>
    </item>
    <item>
      <title>Re: Select n rows before and after an observation based on 2 criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/544500#M150594</link>
      <description>No worries, have a great one!</description>
      <pubDate>Wed, 20 Mar 2019 10:37:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-n-rows-before-and-after-an-observation-based-on-2/m-p/544500#M150594</guid>
      <dc:creator>Criptic</dc:creator>
      <dc:date>2019-03-20T10:37:21Z</dc:date>
    </item>
  </channel>
</rss>

