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

Hi dear experts,

I need to fill entries for entire time period using earlier symbol. For example, I have a company (GVKEY 100) that starts from Dec 2010 and ends at May 2011. Symbol for first date (Dec 2010) is A, then the next one is B which is on April 2011. what i need is to create consecutive; "year" "month" for the entire time period (Dec 2010 to May 2011) and plug in the earlier Symbol for the missing.

 

Please note; I have many of the distinct companies (see below for two GVKEY, 100 and 120). 

 

Data Have;

GVKEYYearMonthSymbol
100201012A
10020114B
10020115BB
120201311D
12020142C
12020144B


Data Want;

GVKEYYearMonthSymbol
100201012A
10020111A
10020112A
10020113A
10020114B
10020115BB
120201311D
120201312D
12020141D
12020142C
12020143C
12020144B

 

thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Some ideas:

  • use "month" and "year" to create a real sas-date, named "date"
  • process the data "by gvkey"
  • calculate the next expected "date" using intnx and store it in a retained variable
  • if date does not match the retained date use a loop to create the missing obs

I didn't provide any code, because you have not provided data in usable form.

View solution in original post

5 REPLIES 5
andreas_lds
Jade | Level 19

Some ideas:

  • use "month" and "year" to create a real sas-date, named "date"
  • process the data "by gvkey"
  • calculate the next expected "date" using intnx and store it in a retained variable
  • if date does not match the retained date use a loop to create the missing obs

I didn't provide any code, because you have not provided data in usable form.

Sandi1
Fluorite | Level 6
Thank you PeterClemmensen
JADE
PeterClemmensen
Tourmaline | Level 20

Try this

 


data have;
input GVKEY $ Year Month Symbol $;
datalines;
100 2010 12 A  
100 2011 4  B  
100 2011 5  BB 
120 2013 11 D  
120 2014 2  C  
120 2014 4  B  
;

data want(keep = GVKEY Year Month Symbol);
   merge have have(firstobs = 2 rename = (GVKEY = g Year = y Month = m) drop = Symbol);

   dt1 = mdy(Month, 1, Year);
   dt2 = mdy(m, 1, y);
   
   i = intck('month', dt1, dt2);

   if GVKEY = g & i > 1 then do j = 0 to i - 1;
      dt = intnx('month', dt1, j);
      Month = month(dt);
      Year = year(dt);
      output;
   end;

   else output;
run;

 

Result:

 

GVKEY  Year  Month  Symbol 
100    2010  12     A 
100    2011  1      A 
100    2011  2      A 
100    2011  3      A 
100    2011  4      B 
100    2011  5      BB 
120    2013  11     D 
120    2013  12     D 
120    2014  1      D 
120    2014  2      C 
120    2014  3      C 
120    2014  4      B 
Sandi1
Fluorite | Level 6
This is the solution
PeterClemmensen
Tourmaline | Level 20

If so, then please mark it as the solution 🙂

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 5 replies
  • 1006 views
  • 2 likes
  • 3 in conversation