{"id":1424,"date":"2019-08-08T17:19:21","date_gmt":"2019-08-08T21:19:21","guid":{"rendered":"http:\/\/kaichen.work\/?p=1424"},"modified":"2019-08-08T17:27:18","modified_gmt":"2019-08-08T21:27:18","slug":"sas-macro-to-count-the-number-of-analysts-following-a-firm","status":"publish","type":"post","link":"https:\/\/www.kaichen.work\/?p=1424","title":{"rendered":"SAS macro to count the number of analysts following a firm"},"content":{"rendered":"<p>This macro is used to count the number of analysts who followed a specific firm. Although this is a commonly used measure in literature, prior studies often give a vague description on what they do. The question is\u2014what does &#8220;analysts following a firm&#8221; really mean?<\/p>\n<p>First, it is only meaningful to count the number at a specified date.<\/p>\n<p>Second, how to define &#8220;an analyst is actually following a firm&#8221;? I use the following definition: if an analyst issued any forecast (EPS or stock price or sales, anything) within a certain window (e.g., 180 days) before the specified date, then the analyst will be counted in. This definition ensures that the analyst is &#8220;actively&#8221; following the firm.<\/p>\n<p>That is why my macro requires two arguments: DATE and WINDOW. This macro is used to answer such question\u2014at a specified date, how many analysts are actively following Firm A, B, &#8230;?<\/p>\n<pre class=\"lang:sas decode:true\">%MACRO ANALYST_COUNT(INFILE=, TICKER=, DATE=, WINDOW=, OUTFILE=);\r\n \r\n\/* This macro is used to count the number of analysts who followed a     *\/\r\n\/* specific firm at a specified date (DATE). Any analyst who issued any  *\/\r\n\/* forecast during the window (WINDOW) before the specified date (DATE)  *\/\r\n\/* will be counted in.                                                   *\/\r\n \r\n\/* This macro use both Detailed History Unadjusted (EPS for US Region)   *\/\r\n\/* and Unadjusted (Non-EPS for US Region). INFILE should contain IBES    *\/\r\n\/* Ticker (TICKER) and DATE.                                             *\/\r\n \r\noptions mprint;\r\n\r\n\/* Stack Detailed History Unadjusted (EPS for US Region) and  *\/\r\n\/* Unadjusted (Non-EPS for US Region).                          *\/\r\ndata detu;\r\n  set ibes.detu_epsus ibes.detu_xepsus;\r\nrun;\r\n\r\n\/* Merge analysts who issued a forecast during the window.  *\/\r\nproc sql;\r\n  create table ibes1\r\n  as select a.*, b.estimator, b.analys, b.anndats\r\n  from (select distinct &amp;TICKER, &amp;DATE from &amp;INFILE) a, detu b\r\n  where not missing(a.&amp;TICKER) and a.&amp;TICKER=b.ticker and \r\n        not missing(a.&amp;DATE) and a.&amp;DATE-&amp;WINDOW+1&lt;=b.anndats&lt;=a.&amp;DATE and\r\n        not missing(b.value);\r\nquit;\r\n\r\n\/* Retain the most recent forecast from a specific analyst. *\/\r\nproc sort data=ibes1; by &amp;TICKER &amp;DATE estimator analys descending anndats; run;\r\nproc sort data=ibes1 out=ibes2 nodupkey; by &amp;TICKER &amp;DATE estimator analys; run;\r\n\r\n\/* Count the number of analysts who issued a forecast during the window.  *\/\r\nproc sql;\r\n  create table ibes3\r\n  as select distinct &amp;TICKER, &amp;DATE, count(anndats) as analyst_count\r\n  from ibes2\r\n  group by &amp;TICKER, &amp;DATE;\r\nquit;\r\n\r\n\/* Merge INFILE with number of analysts  *\/\r\nproc sql;\r\n  create table &amp;OUTFILE\r\n  as select a.*, b.analyst_count\r\n  from &amp;INFILE a left join ibes3 b\r\n  on a.&amp;TICKER=b.&amp;TICKER and a.&amp;DATE=b.&amp;DATE;\r\nquit;\r\n\r\nproc sql;\r\n  drop table detu, ibes1, ibes2;\r\nquit;\r\n\r\n%MEND;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This macro is used to count the number of analysts who followed a specific firm. Although this is a commonly used measure in literature, prior studies often give a vague description on what they do. The question is\u2014what does &#8220;analysts &hellip; <a href=\"https:\/\/www.kaichen.work\/?p=1424\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/1424"}],"collection":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1424"}],"version-history":[{"count":3,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/1424\/revisions"}],"predecessor-version":[{"id":1427,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/1424\/revisions\/1427"}],"wp:attachment":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}