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

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

View solution in original post

3 REPLIES 3
anoopmohandas7
Quartz | Level 8

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 ;
sivakoya
Obsidian | Level 7

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

Patrick
Opal | Level 21

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 720 views
  • 3 likes
  • 3 in conversation