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

Hi, everyone. 

I have a dataset with 3 columns, as shown above. The first column, COD_SEG, represents the internal code for each person. The second column, DT_OCORR, represents the date of a specific event (format yymmd7.). The last column,, CIA_ESCOLHIDA, provides a reference for the corresponding COD_SEG and DT_OCORR. 

My date variable (DT_OCORR) is skipping (jumping) months. I need to insert the missing dates for each COD_SEG. When doing this, I need to fill the other 2 columns with the information from the previous row. Like this.

I have: 

COD_SEGDT_OCORRCIA_ESCOLHIDA
371922021-081806
371922022-021806
371922022-031806
371922022-041806
371922022-061806
371922022-071806
371922022-081806
371922022-091806
371922022-102110
371922022-122110
371922023-012110

 

I need (I have bolded the lines that need to be inserted):

COD_SEGDT_OCORRCIA_ESCOLHIDA
371922021-081806
371922021-091806
371922021-101806
371922021-111806
371922021-121806
371922022-011806
371922022-021806
371922022-031806
371922022-041806
371922022-051806
371922022-061806
371922022-071806
371922022-081806
371922022-091806
371922022-102110
371922022-112110
371922022-122110
371922023-012110

 

My database have so many rows. I need an way to do this for a lot of records. 

Anyone can help me? I'm using SAS Enterprise Guide.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

 

data have;
infile cards expandtabs truncover;
input COD_SEG	DT_OCORR :anydtdte.	CIA_ESCOLHIDA;
format DT_OCORR yymmd7.;
cards;
37192	2021-08 1806
37192	2022-02 1806
37192	2022-03 1806
37192	2022-04 1806
37192	2022-06 1806
37192	2022-07 1806
37192	2022-08 1806
37192	2022-09 1806
37192	2022-10 2110
37192	2022-12 2110
37192	2023-01 2110
;

proc timeseries data = have out = want;
   by COD_SEG;
   id DT_OCORR interval = month
               setmiss  = prev;
   var CIA_ESCOLHIDA;
   format DT_OCORR yymmd7.;
run;

 

 

Result:

 

COD_SEG  DT_OCORR  CIA_ESCOLHIDA
37192    2021-08   1806
37192    2021-09   1806
37192    2021-10   1806
37192    2021-11   1806
37192    2021-12   1806
37192    2022-01   1806
37192    2022-02   1806
37192    2022-03   1806
37192    2022-04   1806
37192    2022-05   1806
37192    2022-06   1806
37192    2022-07   1806
37192    2022-08   1806
37192    2022-09   1806
37192    2022-10   2110
37192    2022-11   2110
37192    2022-12   2110
37192    2023-01   2110

View solution in original post

3 REPLIES 3
Ksharp
Super User
data have;
infile cards expandtabs truncover;
input COD_SEG	DT_OCORR :anydtdte.	CIA_ESCOLHIDA;
format DT_OCORR :yymmd7.;
cards;
37192	2021-08 1806
37192	2022-02 1806
37192	2022-03 1806
37192	2022-04 1806
37192	2022-06 1806
37192	2022-07 1806
37192	2022-08 1806
37192	2022-09 1806
37192	2022-10 2110
37192	2022-12 2110
37192	2023-01 2110
;

proc sort data=have out=temp;
by COD_SEG	DT_OCORR;
run;
data want;
 merge temp temp(firstobs=2 keep=COD_SEG DT_OCORR rename=(COD_SEG=_COD_SEG DT_OCORR=_DT_OCORR));
 output;
 if COD_SEG=_COD_SEG then do;
   do i=1 to intck('month',DT_OCORR,_DT_OCORR)-1;
    DT_OCORR=intnx('month',DT_OCORR,1);output;
   end;
 end;
drop i _COD_SEG _DT_OCORR;
run;
PeterClemmensen
Tourmaline | Level 20

 

data have;
infile cards expandtabs truncover;
input COD_SEG	DT_OCORR :anydtdte.	CIA_ESCOLHIDA;
format DT_OCORR yymmd7.;
cards;
37192	2021-08 1806
37192	2022-02 1806
37192	2022-03 1806
37192	2022-04 1806
37192	2022-06 1806
37192	2022-07 1806
37192	2022-08 1806
37192	2022-09 1806
37192	2022-10 2110
37192	2022-12 2110
37192	2023-01 2110
;

proc timeseries data = have out = want;
   by COD_SEG;
   id DT_OCORR interval = month
               setmiss  = prev;
   var CIA_ESCOLHIDA;
   format DT_OCORR yymmd7.;
run;

 

 

Result:

 

COD_SEG  DT_OCORR  CIA_ESCOLHIDA
37192    2021-08   1806
37192    2021-09   1806
37192    2021-10   1806
37192    2021-11   1806
37192    2021-12   1806
37192    2022-01   1806
37192    2022-02   1806
37192    2022-03   1806
37192    2022-04   1806
37192    2022-05   1806
37192    2022-06   1806
37192    2022-07   1806
37192    2022-08   1806
37192    2022-09   1806
37192    2022-10   2110
37192    2022-11   2110
37192    2022-12   2110
37192    2023-01   2110

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
  • 3 replies
  • 429 views
  • 1 like
  • 3 in conversation