<?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: Adding zeros for rows with no observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336843#M76404</link>
    <description>&lt;P&gt;MIssing years as well as missing quarters?&amp;nbsp; If you know you want a fixed range of years for every ID, then it's relatively straightforward:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Instead of processing data one year at a time, process data one id at a time.&lt;/LI&gt;
&lt;LI&gt;Add an outer loop over years containing the inner loop over quarters.&lt;/LI&gt;
&lt;LI&gt;And instead of a one-dimensional array saving flags for a single year, have a two-dimensional array saving flags for quarters within multiple years:&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=qtr_found:);

  array qtr_found {2000:2001,4} ;

  /* Read one ID to establish qtr_found flags */
  do until (last.id);
    set table1 (keep=id year quarter);
    by id ;
    qtr_found{year,quarter}=1;
  end;

  /* Loop over the flags to control re-reading and output */
  do year=lbound1(qtr_found) to hbound1(qtr_found);
    do quarter=1 to 4;
      if qtr_found{year,quarter} then set table1;
      else value=0;
      output;
    end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Arrays&amp;nbsp;can have multiple dimensions, and those dimensions don't have to have lower bound of 1.&amp;nbsp; Note the first&amp;nbsp;dimension of qtr_found is specified as 2000:2001 which means&amp;nbsp;lower bound of 2000 and upper bound of 2001.&amp;nbsp; But the second dimension is 4, which defaults to lower bound of 1 ... and upper bound of 4.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;The LBOUND1 and HBOUND1 functions provide the lower and upper bounds of dimension 1, so it's easy to directly index the array on year as well as quarter.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 01 Mar 2017 05:17:07 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2017-03-01T05:17:07Z</dc:date>
    <item>
      <title>Adding zeros for rows with no observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336802#M76388</link>
      <description>&lt;P&gt;I have a question regarding adding rows in between another rows. (maybe using do loops?)&lt;/P&gt;&lt;P&gt;I have a data for each quater for each firm. quarters without observations are removed. How can I create rows with zero for those quarters without observations?&lt;/P&gt;&lt;P&gt;For simplicity, let's say there are three firms, for year 2000 and 2001. And I have a data like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input firm_id year quarter value;
cards;
0001 2000 1 10
0001 2000 3 20
0001 2001 2 45
0001 2001 3 14
0001 2001 4 45
0012 2000 1 34
0012 2000 2 10
0047 2000 1 34
0047 2001 3 34
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And eventually I want a data looks like&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/7499i1691B50549EEC0B2/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Capture3.PNG" title="Capture3.PNG" /&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 00:33:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336802#M76388</guid>
      <dc:creator>Sangho</dc:creator>
      <dc:date>2017-03-01T00:33:31Z</dc:date>
    </item>
    <item>
      <title>Re: Adding zeros for rows with no observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336816#M76393</link>
      <description>&lt;P&gt;This can be done by reading each year of data twice.&amp;nbsp; The first read establishes 4 flags, 1 for each quarter, indicating whether that quarter is present in the given year.&amp;nbsp;&amp;nbsp; The second read goes through the 4 flags.&amp;nbsp; If the flag is 1, then read a single record, if not then set value to 0.&amp;nbsp; Then output the record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=qtr_found:);

  array qtr_found {4} ;
  do until (last.year);
    set table1 (keep=id year quarter);
    by id year;
    qtr_found{quarter}=1;
  end;

  do quarter=1 to 4;
    if qtr_found{quarter} then set table1;
    else value=0;
    output;
  end;
run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2017 02:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336816#M76393</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-03-01T02:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: Adding zeros for rows with no observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336831#M76400</link>
      <description>&lt;P&gt;Below picture is the result based on your code.&lt;/P&gt;&lt;P&gt;But for firm_id=12, it has results only for year 2000. I guess it is because in the original data, there was no observation at all for firm 12 in year 2001. But I need 4 rows all with zero for firm 12 for year 2001. How can I fix this problem?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/7500iEEB3BB625CF3F9B1/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Capture4.PNG" title="Capture4.PNG" /&gt;&lt;/P&gt;&lt;P&gt;Thanks again&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 03:50:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336831#M76400</guid>
      <dc:creator>Sangho</dc:creator>
      <dc:date>2017-03-01T03:50:55Z</dc:date>
    </item>
    <item>
      <title>Re: Adding zeros for rows with no observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336832#M76401</link>
      <description>&lt;P&gt;I like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;'s approach, but it didn't account for the missing year. I think the following will:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data table1;
  set table1;
  by firm_id year;
  if last.firm_id and year eq 2000 then do;
    output;
    year=2001;
    quarter=1;
    value=0;
    output;
  end;
  else output;
run;

data want (drop=qtr_found:);
  array qtr_found {4} ;
  do until (last.year);
    set table1 (keep=firm_id year quarter);
    by firm_id year;
    qtr_found{quarter}=1;
  end;

  do quarter=1 to 4;
    if qtr_found{quarter} then set table1;
    else value=0;
    output;
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 04:00:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336832#M76401</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-01T04:00:22Z</dc:date>
    </item>
    <item>
      <title>Re: Adding zeros for rows with no observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336843#M76404</link>
      <description>&lt;P&gt;MIssing years as well as missing quarters?&amp;nbsp; If you know you want a fixed range of years for every ID, then it's relatively straightforward:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Instead of processing data one year at a time, process data one id at a time.&lt;/LI&gt;
&lt;LI&gt;Add an outer loop over years containing the inner loop over quarters.&lt;/LI&gt;
&lt;LI&gt;And instead of a one-dimensional array saving flags for a single year, have a two-dimensional array saving flags for quarters within multiple years:&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=qtr_found:);

  array qtr_found {2000:2001,4} ;

  /* Read one ID to establish qtr_found flags */
  do until (last.id);
    set table1 (keep=id year quarter);
    by id ;
    qtr_found{year,quarter}=1;
  end;

  /* Loop over the flags to control re-reading and output */
  do year=lbound1(qtr_found) to hbound1(qtr_found);
    do quarter=1 to 4;
      if qtr_found{year,quarter} then set table1;
      else value=0;
      output;
    end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Arrays&amp;nbsp;can have multiple dimensions, and those dimensions don't have to have lower bound of 1.&amp;nbsp; Note the first&amp;nbsp;dimension of qtr_found is specified as 2000:2001 which means&amp;nbsp;lower bound of 2000 and upper bound of 2001.&amp;nbsp; But the second dimension is 4, which defaults to lower bound of 1 ... and upper bound of 4.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;The LBOUND1 and HBOUND1 functions provide the lower and upper bounds of dimension 1, so it's easy to directly index the array on year as well as quarter.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 05:17:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336843#M76404</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-03-01T05:17:07Z</dc:date>
    </item>
    <item>
      <title>Re: Adding zeros for rows with no observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336907#M76431</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input firm_id year quarter value;
cards;
0001 2000 1 10
0001 2000 3 20
0001 2001 2 45
0001 2001 3 14
0001 2001 4 45
0012 2000 1 34
0012 2000 2 10
0047 2000 1 34
0047 2001 3 34
;
run;

proc sql;
create table want as
select a.*,coalesce(b.value,0) as value
 from 
 (select * from (select distinct firm_id from table1),
   (select distinct year from table1),
   (select distinct quarter from table1) ) as a
 left join table1 as b 
 on a.firm_id=b.firm_id and a.year=b.year and a.quarter=b.quarter;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2017 10:26:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-zeros-for-rows-with-no-observation/m-p/336907#M76431</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-01T10:26:05Z</dc:date>
    </item>
  </channel>
</rss>

