<?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 Only keep first observation of quarter in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13851#M2070</link>
    <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I'm having troubles finding a solution to my problem. I'm having a dataset with earnings forecasts from a lot of financial analysts for different companies in different quarters. I should be able to filter revisions that are made during one quarter and thus only keep their first forecast.&lt;BR /&gt;
&lt;BR /&gt;
What I already did is creating two new variables (quarter and year). But I don't know how to implement the filtering.&lt;BR /&gt;
&lt;BR /&gt;
What I know:&lt;BR /&gt;
It should delete all observations with forecast date after the first forecast within a given quarter and year. This is the reason I created the two new variables.&lt;BR /&gt;
This filter should be applied to every company and every analyst.&lt;BR /&gt;
&lt;BR /&gt;
I know it's something like this, but I don't know how to translate it into SAS.&lt;BR /&gt;
I thought about creating a counter that would count whether it was the first, second or third forecast in a given quarter and then use a delete statement: if .. &amp;gt; 1 then delete.&lt;BR /&gt;
&lt;BR /&gt;
Your help would be much appreciated!&lt;BR /&gt;
Thanks in advance!</description>
    <pubDate>Sun, 20 Feb 2011 16:54:59 GMT</pubDate>
    <dc:creator>jebuske</dc:creator>
    <dc:date>2011-02-20T16:54:59Z</dc:date>
    <item>
      <title>Only keep first observation of quarter</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13851#M2070</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I'm having troubles finding a solution to my problem. I'm having a dataset with earnings forecasts from a lot of financial analysts for different companies in different quarters. I should be able to filter revisions that are made during one quarter and thus only keep their first forecast.&lt;BR /&gt;
&lt;BR /&gt;
What I already did is creating two new variables (quarter and year). But I don't know how to implement the filtering.&lt;BR /&gt;
&lt;BR /&gt;
What I know:&lt;BR /&gt;
It should delete all observations with forecast date after the first forecast within a given quarter and year. This is the reason I created the two new variables.&lt;BR /&gt;
This filter should be applied to every company and every analyst.&lt;BR /&gt;
&lt;BR /&gt;
I know it's something like this, but I don't know how to translate it into SAS.&lt;BR /&gt;
I thought about creating a counter that would count whether it was the first, second or third forecast in a given quarter and then use a delete statement: if .. &amp;gt; 1 then delete.&lt;BR /&gt;
&lt;BR /&gt;
Your help would be much appreciated!&lt;BR /&gt;
Thanks in advance!</description>
      <pubDate>Sun, 20 Feb 2011 16:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13851#M2070</guid>
      <dc:creator>jebuske</dc:creator>
      <dc:date>2011-02-20T16:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: Only keep first observation of quarter</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13852#M2071</link>
      <description>Hi:&lt;BR /&gt;
  I always find it easier to visualize the data and express the question and the solution in terms of something specific. So, for this specific data:&lt;BR /&gt;
[pre]&lt;BR /&gt;
                           forecast_                forecast_&lt;BR /&gt;
        company    year     quarter     analyst          date    forecast&lt;BR /&gt;
          XXX      2009        1         Anna      01/01/2009     67.9765&lt;BR /&gt;
          XXX      2009        1         Anna      02/01/2009     14.1351&lt;BR /&gt;
          XXX      2009        1         Anna      02/15/2009     14.8816&lt;BR /&gt;
          XXX      2009        1         Anna      03/01/2009     76.3922&lt;BR /&gt;
          XXX      2009        1         Bill      01/15/2009     43.1884&lt;BR /&gt;
          XXX      2009        1         Bill      01/29/2009     55.3036&lt;BR /&gt;
          XXX      2009        1         Bill      02/15/2009     69.1096&lt;BR /&gt;
          XXX      2009        1         Bill      03/29/2009     10.7228&lt;BR /&gt;
          XXX      2009        1         Bill      04/12/2009     22.8308&lt;BR /&gt;
[/pre]&lt;BR /&gt;
  &lt;BR /&gt;
for Company XXX in 2009, Quarter 1, Anna has made 4 forecasts and Bill has made 5 forecasts. Of these forecasts, which ones do you want to KEEP??? From your description, I think you want to keep Anna's 01/01/2009 forecast of 67.9765 and Bill's 01/15/2009 forecast of 43.1884...but I'm not sure.&lt;BR /&gt;
 &lt;BR /&gt;
Can you elaborate using the above data as an example?? If your data does not look like the above data, how does the structure of your data differ from the sample data???&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Sun, 20 Feb 2011 18:41:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13852#M2071</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2011-02-20T18:41:38Z</dc:date>
    </item>
    <item>
      <title>Re: Only keep first observation of quarter</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13853#M2072</link>
      <description>Thanks!&lt;BR /&gt;
&lt;BR /&gt;
My data indeed looks like your example, the only difference is that there is also a company YYY, ZZZ etc.&lt;BR /&gt;
And indeed, i want to keep anna's 01/01 and bill's 01/15 forecasts.&lt;BR /&gt;
Sorry if my question wasn't clear but english is not my native language.</description>
      <pubDate>Sun, 20 Feb 2011 19:00:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13853#M2072</guid>
      <dc:creator>jebuske</dc:creator>
      <dc:date>2011-02-20T19:00:13Z</dc:date>
    </item>
    <item>
      <title>Re: Only keep first observation of quarter</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13854#M2073</link>
      <description>Hi:&lt;BR /&gt;
  So if your data were like this (called QTR1_2009):&lt;BR /&gt;
[pre]&lt;BR /&gt;
             forecast_&lt;BR /&gt;
company year  quarter analyst forecast_date   forecast&lt;BR /&gt;
XXX     2009     1     Anna     01/01/2009     67.9765  &amp;lt;---want&lt;BR /&gt;
XXX     2009     1     Anna     02/01/2009     14.1351&lt;BR /&gt;
XXX     2009     1     Anna     02/15/2009     14.8816&lt;BR /&gt;
XXX     2009     1     Anna     03/01/2009     76.3922&lt;BR /&gt;
XXX     2009     1     Bill     01/15/2009     43.1884  &amp;lt;---want&lt;BR /&gt;
XXX     2009     1     Bill     01/29/2009     55.3036&lt;BR /&gt;
XXX     2009     1     Bill     02/15/2009     69.1096&lt;BR /&gt;
XXX     2009     1     Bill     03/29/2009     10.7228&lt;BR /&gt;
XXX     2009     1     Bill     04/12/2009     22.8308&lt;BR /&gt;
YYY     2009     1     Anna     01/01/2009     42.0122  &amp;lt;---want&lt;BR /&gt;
YYY     2009     1     Anna     01/15/2009     38.5071&lt;BR /&gt;
YYY     2009     1     Anna     02/01/2009     41.7154&lt;BR /&gt;
YYY     2009     1     Anna     02/15/2009     47.9263&lt;BR /&gt;
YYY     2009     1     Anna     03/15/2009     54.1295&lt;BR /&gt;
YYY     2009     1     Bill     01/15/2009     49.7825  &amp;lt;---want&lt;BR /&gt;
YYY     2009     1     Bill     01/29/2009     54.8730&lt;BR /&gt;
YYY     2009     1     Bill     03/01/2009     33.8824&lt;BR /&gt;
YYY     2009     1     Bill     03/15/2009     47.3228&lt;BR /&gt;
ZZZ     2009     1     Anna     01/15/2009     65.1236  &amp;lt;---want&lt;BR /&gt;
ZZZ     2009     1     Anna     02/15/2009     45.2000&lt;BR /&gt;
ZZZ     2009     1     Anna     03/01/2009     56.9731&lt;BR /&gt;
ZZZ     2009     1     Anna     03/15/2009     68.1335&lt;BR /&gt;
ZZZ     2009     1     Bill     02/01/2009     24.5254  &amp;lt;---want&lt;BR /&gt;
ZZZ     2009     1     Bill     03/15/2009     42.1966&lt;BR /&gt;
ZZZ     2009     1     Bill     03/29/2009     32.3020&lt;BR /&gt;
ZZZ     2009     1     Bill     04/12/2009     39.5386&lt;BR /&gt;
[/pre]&lt;BR /&gt;
   &lt;BR /&gt;
Then you would only want to keep the 6 highlighted observations, which happen to occur FIRST when the data are sorted by COMPANY, YEAR, QUARTER and ANALYST (and also probably DATE).&lt;BR /&gt;
&lt;BR /&gt;
This is a job for BY variables, BY-group processing and the fact that a DATA step program can detect whether an observation is the first in a group (by creating some automatic variables) and so, your program can test the automatic variables.&lt;BR /&gt;
&lt;BR /&gt;
 So, for example, for the above data, for 3 companies, for 1 quarter, for 2 analysts, look at the values  for the variables on the right, which have values of 0 or 1:&lt;BR /&gt;
[pre]&lt;BR /&gt;
                                                             first_  first_  first_   first_&lt;BR /&gt;
                  forecast_            forecast_             byvar_  byvar_  byvar_   byvar_&lt;BR /&gt;
   company  year   quarter   analyst        date  forecast  company   year     qtr   analyst&lt;BR /&gt;
     XXX    2009      1       Anna    01/01/2009   67.9765     1        1       1       1&lt;BR /&gt;
     XXX    2009      1       Anna    02/01/2009   14.1351     0        0       0       0&lt;BR /&gt;
     XXX    2009      1       Anna    02/15/2009   14.8816     0        0       0       0&lt;BR /&gt;
     XXX    2009      1       Anna    03/01/2009   76.3922     0        0       0       0&lt;BR /&gt;
     XXX    2009      1       Bill    01/15/2009   43.1884     0        0       0       1&lt;BR /&gt;
     XXX    2009      1       Bill    01/29/2009   55.3036     0        0       0       0&lt;BR /&gt;
     XXX    2009      1       Bill    02/15/2009   69.1096     0        0       0       0&lt;BR /&gt;
     XXX    2009      1       Bill    03/29/2009   10.7228     0        0       0       0&lt;BR /&gt;
     XXX    2009      1       Bill    04/12/2009   22.8308     0        0       0       0&lt;BR /&gt;
     YYY    2009      1       Anna    01/01/2009   42.0122     1        1       1       1&lt;BR /&gt;
     YYY    2009      1       Anna    01/15/2009   38.5071     0        0       0       0&lt;BR /&gt;
     YYY    2009      1       Anna    02/01/2009   41.7154     0        0       0       0&lt;BR /&gt;
     YYY    2009      1       Anna    02/15/2009   47.9263     0        0       0       0&lt;BR /&gt;
     YYY    2009      1       Anna    03/15/2009   54.1295     0        0       0       0&lt;BR /&gt;
     YYY    2009      1       Bill    01/15/2009   49.7825     0        0       0       1&lt;BR /&gt;
     YYY    2009      1       Bill    01/29/2009   54.8730     0        0       0       0&lt;BR /&gt;
     YYY    2009      1       Bill    03/01/2009   33.8824     0        0       0       0&lt;BR /&gt;
     YYY    2009      1       Bill    03/15/2009   47.3228     0        0       0       0&lt;BR /&gt;
     ZZZ    2009      1       Anna    01/15/2009   65.1236     1        1       1       1&lt;BR /&gt;
     ZZZ    2009      1       Anna    02/15/2009   45.2000     0        0       0       0&lt;BR /&gt;
     ZZZ    2009      1       Anna    03/01/2009   56.9731     0        0       0       0&lt;BR /&gt;
     ZZZ    2009      1       Anna    03/15/2009   68.1335     0        0       0       0&lt;BR /&gt;
     ZZZ    2009      1       Bill    02/01/2009   24.5254     0        0       0       1&lt;BR /&gt;
     ZZZ    2009      1       Bill    03/15/2009   42.1966     0        0       0       0&lt;BR /&gt;
     ZZZ    2009      1       Bill    03/29/2009   32.3020     0        0       0       0&lt;BR /&gt;
     ZZZ    2009      1       Bill    04/12/2009   39.5386     0        0       0       0&lt;BR /&gt;
[/pre]&lt;BR /&gt;
The variables were created by capturing the "first.byvar" values into a variable that could be displayed in PROC PRINT -- in the following manner:&lt;BR /&gt;
  first_byvar_company was created from first.company&lt;BR /&gt;
  first_byvar_year was created from first.year&lt;BR /&gt;
  first_byvar_qtr was created from first.forecast_quarter&lt;BR /&gt;
  first_byvar_analyst was created from first.analyst&lt;BR /&gt;
                   &lt;BR /&gt;
The first observation for each analyst -- the ones you want -- will be able to be discovered by testing whether FIRST.ANALYST is equal to 1 and, if it is, then written to a new dataset with a program like:&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sort data=qtr1_2009 out=qtr1_2009;&lt;BR /&gt;
  by company year forecast_quarter analyst forecast_date;&lt;BR /&gt;
run;&lt;BR /&gt;
                                  &lt;BR /&gt;
data keepfirst;&lt;BR /&gt;
  set qtr1_2009;&lt;BR /&gt;
  by company year forecast_quarter analyst;&lt;BR /&gt;
  if first.analyst then output keepfirst;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                            &lt;BR /&gt;
(when the data are sorted and the correct by variables are specified). Because of the way the BY variables were sorted, every time COMPANY changes, all the nested FIRST.byvar values are reset -- because if it is the FIRST.COMPANY, it is also the FIRST.YEAR and FIRST.ANALYST etc for that company.&lt;BR /&gt;
          &lt;BR /&gt;
For a more thorough explanation, you will want to read through the ENTIRE topic entitled "BY-Group Processing in the DATA Step"&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a001283274.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a001283274.htm&lt;/A&gt;&lt;BR /&gt;
     &lt;BR /&gt;
The program that produced the outputs above are shown below.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
** make some data;&lt;BR /&gt;
data qtr1_2009;&lt;BR /&gt;
  infile datalines dlm=',' dsd;&lt;BR /&gt;
  input company $ year forecast_quarter analyst $ forecast_date : mmddyy10. forecast;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
XXX,2009,1,Anna,01/01/2009,67.9765&lt;BR /&gt;
XXX,2009,1,Anna,02/01/2009,14.1351&lt;BR /&gt;
XXX,2009,1,Anna,02/15/2009,14.8816&lt;BR /&gt;
XXX,2009,1,Anna,03/01/2009,76.3922&lt;BR /&gt;
XXX,2009,1,Bill,01/15/2009,43.1884&lt;BR /&gt;
XXX,2009,1,Bill,01/29/2009,55.3036&lt;BR /&gt;
XXX,2009,1,Bill,02/15/2009,69.1096&lt;BR /&gt;
XXX,2009,1,Bill,03/29/2009,10.7228&lt;BR /&gt;
XXX,2009,1,Bill,04/12/2009,22.8308&lt;BR /&gt;
YYY,2009,1,Anna,01/01/2009,42.0122&lt;BR /&gt;
YYY,2009,1,Anna,01/15/2009,38.5071&lt;BR /&gt;
YYY,2009,1,Anna,02/01/2009,41.7154&lt;BR /&gt;
YYY,2009,1,Anna,02/15/2009,47.9263&lt;BR /&gt;
YYY,2009,1,Anna,03/15/2009,54.1295&lt;BR /&gt;
YYY,2009,1,Bill,01/15/2009,49.7825&lt;BR /&gt;
YYY,2009,1,Bill,01/29/2009,54.8730&lt;BR /&gt;
YYY,2009,1,Bill,03/01/2009,33.8824&lt;BR /&gt;
YYY,2009,1,Bill,03/15/2009,47.3228&lt;BR /&gt;
ZZZ,2009,1,Anna,01/15/2009,65.1236&lt;BR /&gt;
ZZZ,2009,1,Anna,02/15/2009,45.2000&lt;BR /&gt;
ZZZ,2009,1,Anna,03/01/2009,56.9731&lt;BR /&gt;
ZZZ,2009,1,Anna,03/15/2009,68.1335&lt;BR /&gt;
ZZZ,2009,1,Bill,02/01/2009,24.5254&lt;BR /&gt;
ZZZ,2009,1,Bill,03/15/2009,42.1966&lt;BR /&gt;
ZZZ,2009,1,Bill,03/29/2009,32.3020&lt;BR /&gt;
ZZZ,2009,1,Bill,04/12/2009,39.5386&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                               &lt;BR /&gt;
** Sort the data just to be sure that it is in the correct order;&lt;BR /&gt;
proc sort data=qtr1_2009 out=qtr1_2009;&lt;BR /&gt;
  by company year forecast_quarter analyst forecast_date;&lt;BR /&gt;
run;&lt;BR /&gt;
                        &lt;BR /&gt;
** This dataset is just to show how FIRST.byvar values are created automatically;&lt;BR /&gt;
** And their values are captured into variables for display by PROC PRINT;&lt;BR /&gt;
data showall;&lt;BR /&gt;
  set qtr1_2009;&lt;BR /&gt;
  by company year forecast_quarter analyst;&lt;BR /&gt;
  first_byvar_company = first.company;&lt;BR /&gt;
  first_byvar_year = first.year;&lt;BR /&gt;
  first_byvar_qtr = first.forecast_quarter;&lt;BR /&gt;
  first_byvar_analyst = first.analyst;&lt;BR /&gt;
  output showall;&lt;BR /&gt;
run; &lt;BR /&gt;
                          &lt;BR /&gt;
** Show values of all BY vars automatic variables, as captured for PRINT.;&lt;BR /&gt;
proc print data=showall noobs;&lt;BR /&gt;
  title 'showall -- look at values created by using FIRST.byvar';&lt;BR /&gt;
   var company year forecast_quarter analyst forecast_date forecast &lt;BR /&gt;
       first_byvar_company  first_byvar_year  first_byvar_qtr  first_byvar_analyst   ;&lt;BR /&gt;
   format forecast_date mmddyy10.;&lt;BR /&gt;
run;&lt;BR /&gt;
                        &lt;BR /&gt;
** Now get only the observations of interest using FIRST.ANALYST to control output.;&lt;BR /&gt;
data keepfirst;&lt;BR /&gt;
  set qtr1_2009;&lt;BR /&gt;
  by company year forecast_quarter analyst;&lt;BR /&gt;
  if first.analyst then output keepfirst;&lt;BR /&gt;
run;&lt;BR /&gt;
                &lt;BR /&gt;
** Display the observations in the new dataset. QTR1_2009 still has the original group of observations.;&lt;BR /&gt;
** But WORK.KEEPFIRST is what you would use going forward for more analysis.;&lt;BR /&gt;
proc print data=keepfirst noobs;&lt;BR /&gt;
  title 'keepfirst';&lt;BR /&gt;
   var company year forecast_quarter analyst forecast_date forecast ;&lt;BR /&gt;
   format forecast_date mmddyy10.;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Sun, 20 Feb 2011 19:52:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13854#M2073</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2011-02-20T19:52:46Z</dc:date>
    </item>
    <item>
      <title>Re: Only keep first observation of quarter</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13855#M2074</link>
      <description>proc sort data=a;&lt;BR /&gt;
by analyst company quarter;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data a2;&lt;BR /&gt;
set a;&lt;BR /&gt;
if first.company  then a=1;&lt;BR /&gt;
if first.quarter then b=1;&lt;BR /&gt;
if first.analyst then c=1;&lt;BR /&gt;
by analyst company quarter;&lt;BR /&gt;
if a=1 and b=1  then output;&lt;BR /&gt;
run;</description>
      <pubDate>Mon, 21 Feb 2011 05:40:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13855#M2074</guid>
      <dc:creator>R_Win</dc:creator>
      <dc:date>2011-02-21T05:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: Only keep first observation of quarter</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13856#M2075</link>
      <description>Thanks a lot!</description>
      <pubDate>Mon, 21 Feb 2011 16:10:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-keep-first-observation-of-quarter/m-p/13856#M2075</guid>
      <dc:creator>jebuske</dc:creator>
      <dc:date>2011-02-21T16:10:54Z</dc:date>
    </item>
  </channel>
</rss>

