Solved
Contributor
Posts: 30

# Get last months record for id and insert back in dataset for current month

Hi,

I have following monthly dataset

month_end   account_nbr     x     y     z

30sep16               1            100    Y    1

31oct16                1             50     Y    1

30nov16               1             25     Y    1

31dec16               1             10     Y    1

30sep16               2            100    Y    1

31oct16                2             50     Y    1

30nov16               2             0       Y    1

Account_nbr '2' is closed in nov and monthly dataset will not have a record for 2 in dec.

This is a huge dataset with millions of records across several years. I am looking to create a dummy row for account_nbr '2' for the latest month end i.e 31dec16 with the values from nov. please suggest a easy way to do this.

Resulting dataset should look like:

month_end   account_nbr     x     y     z

30sep16               1            100    Y    1

31oct16                1             50     Y    1

30nov16               1             25     Y    1

31dec16               1             10     Y    1

30sep16               2            100    Y    1

31oct16                2             50     Y    1

30nov16               2             0       Y    1

31dec16               2             0       Y    1

Thanks

Accepted Solutions
Solution
‎01-25-2017 07:25 PM
Posts: 4,797

## Re: Get last months record for id and insert back in dataset for current month

If it's only about repeating data up to a given month then below should work.

``````data have;
input month_end date7. acct_nbr x;
format month_end date7.;
datalines;
30sep16 1 100
31oct16 1 50
30nov16 1 25
30dec16 1 10
30sep16 2 100
31oct16 2 0
;
run;

proc sort data=have out=want;
by acct_nbr month_end;
run;

data want;
set want;
by acct_nbr month_end;
output;
if last.acct_nbr then
do while(month_end < '31dec2016'd);
month_end=intnx('month',month_end,1,'e');
output;
end;
run;
``````

All Replies
Contributor
Posts: 44

## Re: Get last months record for id and insert back in dataset for current month

Sivakoya,

The below would certainely need to be optimized. But this shoud do the purpose.

Good Luck.

``````data test ;
input month_end date7.  acct_nbr x ;
datalines ;
30sep16 1 100
31oct16  1 50
30nov16 1 25
30dec16 1 10
31oct16 2  50
30nov16 2 0
;
run ;

data test2;
set test ;
by acct_nbr ;
Y=last.acct_nbr ;
format month_end date10. ;
run ;

proc sql;
create table test3 as
select *
from test2
where Y=1 ;
quit ;

proc sql;
create table test4 as
select intnx('month',month_end,1,'end') as month_end format=date10.,acct_nbr,x
from test3
where  month(month_end) <> month(intnx('month',today(),-1,'end')) ;
quit ;

data test5 ;
set test4 test ;
run ;

proc sql ;
select month_end format=date10.,acct_nbr,x
from test5
order by 2,1;quit ;``````
Contributor
Posts: 30

## Re: Get last months record for id and insert back in dataset for current month

Thanks for your response @anoopmohandas7 . Helped me directionally. Not sure if there is a procedure out there for performing this kind of data manipulation.

Code you suggested would only create one subsequent record.

But if nov month end record is also missing for id '2'. i would need to create records for nov and dec. since dec is the latest month end in my dataset.

data test ;
input month_end date7.  acct_nbr x ;
datalines ;
30sep16 1 100
31oct16  1 50
30nov16 1 25
31dec16 1 10
31oct16 2  50
;
run ;

need:

30sep16 1 100
31oct16  1 50
30nov16 1 25
31dec16 1 10
31oct16 2  50

30nov16 2 50

31dec16 2 50

Have to duplicate oct record till max(month_end).

Thanks

Siva

Solution
‎01-25-2017 07:25 PM
Posts: 4,797

## Re: Get last months record for id and insert back in dataset for current month

If it's only about repeating data up to a given month then below should work.

``````data have;
input month_end date7. acct_nbr x;
format month_end date7.;
datalines;
30sep16 1 100
31oct16 1 50
30nov16 1 25
30dec16 1 10
30sep16 2 100
31oct16 2 0
;
run;

proc sort data=have out=want;
by acct_nbr month_end;
run;

data want;
set want;
by acct_nbr month_end;
output;
if last.acct_nbr then
do while(month_end < '31dec2016'd);
month_end=intnx('month',month_end,1,'e');
output;
end;
run;
``````
☑ This topic is solved.