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
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;
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 ;
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
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.