BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dsriggs
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

ballardw
Super User

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.

dsriggs
Fluorite | Level 6

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.

ballardw
Super User

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.

dsriggs
Fluorite | Level 6

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

Patrick
Opal | Level 21

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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 3418 views
  • 0 likes
  • 4 in conversation