## Repeating N number of observations in data step

Solved
Occasional Contributor
Posts: 19

# Repeating N number of observations in data step

I have a work file with 12 observations one for each month, and then for future months I have blank or null values.  What I want to do is repeat those 12 observations for all of the blank or null values.  I know about the retain function, but this just carries down the last observation, how can I repeat the last 12?  If this can be done in data step that would be preferable.

Accepted Solutions
Solution
‎10-12-2016 10:16 AM
Posts: 4,545

## Re: Repeating N number of observations in data step

Here you go.

``````data have;
input month_begin_dt :mmddyy10. x :best32.;
format month_begin_dt date9.;
datalines;
1/1/2013 0.65
2/1/2013 0.96
3/1/2013 0.07
4/1/2013 0.15
5/1/2013 0.90
6/1/2013 0.34
7/1/2013 0.10
8/1/2013 0.42
9/1/2013 0.09
10/1/2013 0.28
11/1/2013 0.31
12/1/2013 0.79
;
run;

/** option 1 **/
data template;
month_begin_dt='01jan2013'd;
format month_begin_dt date9.;
do while(month_begin_dt<='01dec2014'd);
output;
month_begin_dt=intnx('month',month_begin_dt,1,'b');
end;
run;

proc sql;
create table want1 as
select b.month_begin_dt, a.x
from have as A inner join template as B
on month(a.month_begin_dt)=month(b.month_begin_dt)
order by month_begin_dt
;
quit;

/** option 2 **/
data want2;
set have;
output;
month_begin_dt=intnx('month',month_begin_dt,12,'b');
output;
run;

proc sort data=want2;
by month_begin_dt;
run;

``````

All Replies
Super User
Posts: 9,211

## Re: Repeating N number of observations in data step

It is called a do loop:

```data want;
set have;
do i=1 to 10;
output;
end;
run;
```

This is just an example, each record in have gets output 10 times.  If you post example test data (in the form of a datastep) and what you want the output to look like the code can be customised.

Super User
Posts: 13,034

## Re: Repeating N number of observations in data step

If @RW9's solution does not match you needs please provide an example starting set and the desired result.

Also the solution may require sorting to get into a specific order.

Occasional Contributor
Posts: 19

## Re: Repeating N number of observations in data step

I attached a simplified version of what I want.  The first tab is where I am at, and the second is where I hope to end up through data step.

Super User
Posts: 13,034

## Re: Repeating N number of observations in data step

Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.

This link shows instructions on how you can create datastep code from your SAS dataset to provide data that we can test code against or fully understand the contents: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

Paste the text or attach as a text file.

Occasional Contributor
Posts: 19

## Re: Repeating N number of observations in data step

I can't download the zip file from that link on my work computer.  Here is a text version of what I have and what I want. No downloading required.  Both month and 'x' are of type numeric.

Have:

month x
1/1/2013 0.65
2/1/2013 0.96
3/1/2013 0.07
4/1/2013 0.15
5/1/2013 0.90
6/1/2013 0.34
7/1/2013 0.10
8/1/2013 0.42
9/1/2013 0.09
10/1/2013 0.28
11/1/2013 0.31
12/1/2013 0.79

Want:

month x
1/1/2013 0.65
2/1/2013 0.96
3/1/2013 0.07
4/1/2013 0.15
5/1/2013 0.90
6/1/2013 0.34
7/1/2013 0.10
8/1/2013 0.42
9/1/2013 0.09
10/1/2013 0.28
11/1/2013 0.31
12/1/2013 0.79
1/1/2014 0.65
2/1/2014 0.96
3/1/2014 0.07
4/1/2014 0.15
5/1/2014 0.90
6/1/2014 0.34
7/1/2014 0.10
8/1/2014 0.42
9/1/2014 0.09
10/1/2014 0.28
11/1/2014 0.31
12/1/2014 0.79

Solution
‎10-12-2016 10:16 AM
Posts: 4,545

## Re: Repeating N number of observations in data step

Here you go.

``````data have;
input month_begin_dt :mmddyy10. x :best32.;
format month_begin_dt date9.;
datalines;
1/1/2013 0.65
2/1/2013 0.96
3/1/2013 0.07
4/1/2013 0.15
5/1/2013 0.90
6/1/2013 0.34
7/1/2013 0.10
8/1/2013 0.42
9/1/2013 0.09
10/1/2013 0.28
11/1/2013 0.31
12/1/2013 0.79
;
run;

/** option 1 **/
data template;
month_begin_dt='01jan2013'd;
format month_begin_dt date9.;
do while(month_begin_dt<='01dec2014'd);
output;
month_begin_dt=intnx('month',month_begin_dt,1,'b');
end;
run;

proc sql;
create table want1 as
select b.month_begin_dt, a.x
from have as A inner join template as B
on month(a.month_begin_dt)=month(b.month_begin_dt)
order by month_begin_dt
;
quit;

/** option 2 **/
data want2;
set have;
output;
month_begin_dt=intnx('month',month_begin_dt,12,'b');
output;
run;

proc sort data=want2;
by month_begin_dt;
run;

``````
☑ This topic is solved.