<?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: How to delete first five and last five observations from each variable in a dataset in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524584#M26586</link>
    <description>&lt;P&gt;Thank you for you response. Your code only removes the first five and last five observation of the whole dataset which is over 3000 observations.&lt;/P&gt;&lt;P&gt;What I want is to remove first five and last five observation for each bridge which has 25 observation each. The dataset of 120 bridges makes the 3000 observations. I only want to retain the middle 15 observation for each bridge. The STRUCTURE _NUMBER_008 is a mix of character and numeric. The dataset is ordered based on the STRUCTURE_NUMBER_008 and Inspection_Year.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
    <pubDate>Fri, 04 Jan 2019 15:36:11 GMT</pubDate>
    <dc:creator>mmhxc5</dc:creator>
    <dc:date>2019-01-04T15:36:11Z</dc:date>
    <item>
      <title>How to delete first five and last five observations from a dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524440#M26579</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a dataset of over 3000 observations for bridges inspected since 1992 -2017. Each bridge is inspected once each year. I would like to delete the first five years and last five years of inspection result for each bridge. Chosing STRUCTURE_NUMBER_008 or Inspection_Year might work as the variable to delete, because the beginning and last year of inspection is not the same for all bridges. The attached image shows two bridge IDs sorted by STRUCTURE_NUMBER_008 (First column) and Inspection_Year(Last column). STRUCTURE_NUMBER_008 is random name given for each bridge. I would be glad if anyone could help me to do this task. I use SAS 9.4 in Windows 10 and I have the dataset as permanent SAS files.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/25978i2E9B762734E44505/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jan 2019 04:34:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524440#M26579</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-04T04:34:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to delete first five and last five observations from a dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524444#M26580</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do p=6 to nobs-5;
   set have nobs=nobs point=p;
    output;
end;
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Jan 2019 05:25:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524444#M26580</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-04T05:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to delete first five and last five observations from each variable in a dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524584#M26586</link>
      <description>&lt;P&gt;Thank you for you response. Your code only removes the first five and last five observation of the whole dataset which is over 3000 observations.&lt;/P&gt;&lt;P&gt;What I want is to remove first five and last five observation for each bridge which has 25 observation each. The dataset of 120 bridges makes the 3000 observations. I only want to retain the middle 15 observation for each bridge. The STRUCTURE _NUMBER_008 is a mix of character and numeric. The dataset is ordered based on the STRUCTURE_NUMBER_008 and Inspection_Year.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jan 2019 15:36:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524584#M26586</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-04T15:36:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to delete first five and last five observations from each variable in a dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524600#M26587</link>
      <description>&lt;P&gt;Please follow the comments and logic and see if this helps&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Creating a sample data with 120 bridges with each bridge having 25 observations*/
data sample;
do bridge=1 to 120;
	do var=1 to 25;
	output;
	end;
end;
run;

/*Output */
data want;
/*1st loop count the number of records in each bridge*/
do n=1 by 1 until(last.bridge);
set sample;
by bridge;
end;
/*2nd loop remove first five and last five observation for each bridge 
and output the rest*/
do _n_=1 by 1 until(last.bridge);
set sample;
by bridge;
if 6&amp;lt;=_n_&amp;lt;= n-5 then output;
end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Jan 2019 16:25:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524600#M26587</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-04T16:25:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to delete first five and last five observations from a dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524797#M26599</link>
      <description>&lt;P&gt;It is forwards and backwards tricks .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
do bridge=1 to 10;
	do var=1 to 25;
	output;
	end;
end;
run;

data want;
 merge sample sample(firstobs=6 keep=bridge rename=(bridge=_bridge));
 if bridge ne lag5(bridge) or bridge ne _bridge then delete;
 drop _bridge;
 run;

proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 05 Jan 2019 11:09:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524797#M26599</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-01-05T11:09:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to delete first five and last five observations from each variable in a dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524804#M26600</link>
      <description>&lt;P&gt;If you are not familiar with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;'s BY-group trick, see &lt;A href="https://blogs.sas.com/content/iml/2018/02/26/how-to-use-first-variable-and-last-variable-in-a-by-group-analysis-in-sas.html" target="_self"&gt;"How to use FIRST.variable and LAST.variable in a BY-group analysis in SAS."&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Jan 2019 13:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/524804#M26600</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-01-05T13:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to delete first five and last five observations from each variable in a dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/525222#M26615</link>
      <description>&lt;P&gt;Thank you for your timely reply. I really appreciate it. Based on your code I created a "Sample" data, then I defined the "want" and also the set "sample" to the end of your code. How can I link this code to the Bridge dataset I have? I mean, which part of your code will call the bridge dataset which has all the data that I want to process?&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;</description>
      <pubDate>Mon, 07 Jan 2019 20:53:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/525222#M26615</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-07T20:53:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to delete first five and last five observations from a dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/525225#M26616</link>
      <description>&lt;P&gt;Thank you for your timely reply. I really appreciate it. There is only two data calling in your code,&amp;nbsp; "sample" and "want". How can I link this code to the Bridge dataset I have? I mean, which part of your code will call the bridge dataset which has all the data that I want to process?&lt;/P&gt;</description>
      <pubDate>Mon, 07 Jan 2019 20:58:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/525225#M26616</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-07T20:58:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to delete first five and last five observations from a dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/525305#M26617</link>
      <description>&lt;P&gt;Both&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;created a file called sample to represent your bridge dataset. They did not expect you to create a sample dataset but, rather, replace it with your actual bridge dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, you have to decide whether you want to drop the first and last 5 records for each structure_no_008 OR, for each&amp;nbsp;&lt;SPAN&gt;structure_no_008, you want to drop any inspection_year that was less than the earliest+4 and/or greater than the last inspection_year -4.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;My suggested code does the latter:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data bridge (drop=structure_no var);
  do structure_no=1 to 120;
    structure_no_008=catt('000000000000H',put(structure_no,z3.));
    inspection_year=1996;
    do var=1 to 25;
      inspection_year+1;
      if mod(structure_no,40) eq 0 then do;
        if inspection_year ne 2000 then output;
      end;
      else output;
    end;
  end;
run;

/*Output */
data want (drop=start last);
/*1st loop count the number of records in each bridge*/
  do until(last.structure_no_008);
    set bridge;
    by structure_no_008;
    if first.structure_no_008 then start=inspection_year;
    else if last.structure_no_008 then last=inspection_year;
  end;
/*2nd loop only keep inspection_years if they are greater than the
  earliest inspection_year+4 and lower than latest inspection-4*/
  do until(last.structure_no_008);
    set bridge;
    by structure_no_008;
    if start+5 &amp;lt;=inspection_year&amp;lt;=last-5 then output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Art, CEO, AnalystFinder.com&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jan 2019 01:36:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/525305#M26617</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2019-01-08T01:36:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to delete first five and last five observations from a dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/525321#M26619</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;, and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt; for your help. Appreciated. &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt; your clarification was very helpful.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jan 2019 04:00:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/525321#M26619</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-08T04:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to delete first five and last five observations from a dataset</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/527229#M26665</link>
      <description>&lt;P&gt;‘Sample' dataset is '&lt;SPAN&gt;Bridge' dataset ,according to Art.T 's code.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jan 2019 07:40:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-delete-first-five-and-last-five-observations-from-a/m-p/527229#M26665</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-01-15T07:40:53Z</dc:date>
    </item>
  </channel>
</rss>

