Announcement

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

  • Using loops

    Hello,

    I am trying to write these commands in a more concise form, is it applicable to use loops here?
    Attached Files

  • #2
    why do you want 12 indicator variables? if you want these for use in estimation of a model, you can probably use factor variable notation; see
    Code:
    h fvvarlist
    if, for some reason, you actually need all these indicators, use -tab- with the "gen" option; see
    Code:
    h tab1

    Comment


    • #3
      Hello Rich, thanks for your response. I am solving a stata exercise for my course, and it states that "The dataset should have one column for every month (ordered from January through December) and include year 2020 exports only."

      I used the tab months, gen command and was so helpful, but I had to do several steps to rename the generated months. I was wondering if I could use a better code to rename in less steps ?

      Comment


      • #4
        I would like the setter of the exercise to explain why they think this is good style, but it can be done concisely. Years ago at my suggestion StataCorp wired in some often used little lists as accessible text. Type

        Code:
        creturn list
        and scroll briskly to the end.


        Code:
        clear
        
        set obs 12 
        
        gen month = _n 
        
        tokenize `c(Mons)' 
        
        forval m = 1/12 { 
            gen ``m'' = month == `m'
        }
        
        l, sep(6)
        
             +-------------------------------------------------------------------------------+
             | month   Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec |
             |-------------------------------------------------------------------------------|
          1. |     1     1     0     0     0     0     0     0     0     0     0     0     0 |
          2. |     2     0     1     0     0     0     0     0     0     0     0     0     0 |
          3. |     3     0     0     1     0     0     0     0     0     0     0     0     0 |
          4. |     4     0     0     0     1     0     0     0     0     0     0     0     0 |
          5. |     5     0     0     0     0     1     0     0     0     0     0     0     0 |
          6. |     6     0     0     0     0     0     1     0     0     0     0     0     0 |
             |-------------------------------------------------------------------------------|
          7. |     7     0     0     0     0     0     0     1     0     0     0     0     0 |
          8. |     8     0     0     0     0     0     0     0     1     0     0     0     0 |
          9. |     9     0     0     0     0     0     0     0     0     1     0     0     0 |
         10. |    10     0     0     0     0     0     0     0     0     0     1     0     0 |
         11. |    11     0     0     0     0     0     0     0     0     0     0     1     0 |
         12. |    12     0     0     0     0     0     0     0     0     0     0     0     1 |
             +-------------------------------------------------------------------------------+
        That is, the names Jan Feb ... Nov Dec are wired into c(Mons).

        .
        Note the crucial detail of creating (0, 1) indicator variables. (1, .) indicator variables are more or less useless. Sometimes people follow

        gen foo = 1 if whatever

        with some analogue of

        replace foo = 0 if !whatever

        where whatever is a true or false condition, but

        gen foo = whatever

        gets you there in one.

        More at

        https://journals.sagepub.com/doi/pdf...867X0400400213

        https://journals.sagepub.com/doi/pdf...36867X19830921 especially Section 5.


        Comment


        • #5
          Thank you so much Nick for the informative response, much appreciated!

          Comment

          Working...
          X