## Adding zeros for rows with no observation

Solved
Occasional Contributor
Posts: 15

# Adding zeros for rows with no observation

I have a question regarding adding rows in between another rows. (maybe using do loops?)

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?

For simplicity, let's say there are three firms, for year 2000 and 2001. And I have a data like this:

``````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;``````

And eventually I want a data looks like

Thanks!

Accepted Solutions
Solution
‎03-01-2017 10:59 AM
Posts: 1,387

## Re: Adding zeros for rows with no observation

MIssing years as well as missing quarters?  If you know you want a fixed range of years for every ID, then it's relatively straightforward:

1. Instead of processing data one year at a time, process data one id at a time.
2. Add an outer loop over years containing the inner loop over quarters.
3. 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:

``````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;
``````

Notes:

1. Arrays can have multiple dimensions, and those dimensions don't have to have lower bound of 1.  Note the first dimension of qtr_found is specified as 2000:2001 which means lower bound of 2000 and upper bound of 2001.  But the second dimension is 4, which defaults to lower bound of 1 ... and upper bound of 4.

2. 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.

All Replies
Posts: 1,387

## Re: Adding zeros for rows with no observation

This can be done by reading each year of data twice.  The first read establishes 4 flags, 1 for each quarter, indicating whether that quarter is present in the given year.   The second read goes through the 4 flags.  If the flag is 1, then read a single record, if not then set value to 0.  Then output the record.

``````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;

``````
Occasional Contributor
Posts: 15

## Re: Adding zeros for rows with no observation

[ Edited ]

Below picture is the result based on your code.

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?

Thanks again

Super User
Posts: 8,213

## Re: Adding zeros for rows with no observation

I like @mkeintz's approach, but it didn't account for the missing year. I think the following will:

```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;
```

Art, CEO, AnalystFinder.com

Solution
‎03-01-2017 10:59 AM
Posts: 1,387

## Re: Adding zeros for rows with no observation

MIssing years as well as missing quarters?  If you know you want a fixed range of years for every ID, then it's relatively straightforward:

1. Instead of processing data one year at a time, process data one id at a time.
2. Add an outer loop over years containing the inner loop over quarters.
3. 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:

``````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;
``````

Notes:

1. Arrays can have multiple dimensions, and those dimensions don't have to have lower bound of 1.  Note the first dimension of qtr_found is specified as 2000:2001 which means lower bound of 2000 and upper bound of 2001.  But the second dimension is 4, which defaults to lower bound of 1 ... and upper bound of 4.

2. 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.

Super User
Posts: 10,844

## Re: Adding zeros for rows with no observation

``````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;``````
☑ This topic is solved.