Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with creating for loop

    Hello Statalist!

    I am working with data that looks like this:

    analys ticker
    72 ATX
    72 DMV
    81 DMV
    81 AZW
    96 DMT

    I am trying to create a dataset that, as a first step, has an observation for every day of the period (1Jan2007-31Dec2020) corresponding to each of these observations, like this

    analys ticker date
    72 ATX 1 Jan 2007
    72 ATX 2 Jan 2007
    .
    .
    .
    72 ATX 31 Dec 2020
    72 DMV 1 Jan 2007
    72 DMV 2 Jan 2007
    .
    .
    .​​​​​​​
    72 DMV 31 Dec 2020

    and so on. This is only the first step and I will later also have to merge in other data, create running indicator variables, etc. and because the file size will blow up I have resorted to doing this via a for loop. I used the following code:

    egen analysbyfirm = concat(ticker analys)


    levelsof analysbyfirm, local(tickeranalyslevels)
    foreach i of local tickeranalyslevels {
    preserve
    drop if analysbyfirm != `i'
    expand 5114
    gen date = mdy(12, 31, 2006) +_n
    format date %td
    append using experiment.dta
    save experiment.dta, replace
    restore
    }


    but at the end of the for loop Stata returns the error:

    00BV70 invalid name
    r(198);


    I am very new to Stata and completely at a loss, any help would be deeply appreciated! Thank you!

  • #2
    This might as simple as

    Code:
    egen long id = group(analys ticker) 
    compress id 
    
    tsset id date 
    tsfill
    but that code could be scuppered by complications, for example if date is not a Stata numeric date variable.

    It should certainly not be necessary to loop over all stocks with separate files for each;

    Whether this is a good idea is a different question, as it will just imply lots of observations with missing values for weekends, public holidays, and otherwise if any stock was not traded over all of this period.

    Comment


    • #3
      Hi Nick, thank you so much for your response, this really helps! The non-trading days should not be an issue as I'm also merging in a set of "incident dates" which are not restricted to trading days, but this gives me a lot to think about with regards to overall data structure.

      Comment

      Working...
      X