Announcement

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

  • Creating Period Variables

    This is what I Currently Have
    unemployment period
    x 1
    y 2
    z 3
    w 4
    t 5
    With this code, this is what i get.

    Code:
    levelsof period, local(levels)
    foreach i of local levels {
    
    gen unemployment`i'=unemployment if period==`i'
    
    }
    unemployment1 unemployment2 unemployment3 unemployment4 unemployment5 period
    x . . . . 1
    . y . . . 2
    . . z . 3
    . . w . 4
    . . . . t 5
    But this is what I want and I can't figure out how.
    unemployment1 unemployment2 unemployment3 unemployment4 unemployment5 period
    x y z w t 1
    x y z w t 2
    x y z w t 3
    x y z w t 4
    x y z w t 5
    besides all of this, i have different zones (zone1 zone2 zone3) e.g city, town, region, whatever.

    so my code to get the latter table it's something like this, and i would like to know if it's fine (continuing the previous code)

    Code:
    levelsof period, local(levels)
    foreach i of local levels {
    
    gen unemployment`i'=unemployment if period==`i', by(period zone1 zone2 zone3)
    
    gen unemployment`i'mean=mean(unemployment), by(period zone1 zone2 zone3)
    
    
    
    replace unemployment`i'=unemployment`i'mean if unemployment==.
    
    drop unemployment`i'mean
    
    }

    Any help? hope I made my self clear.

    Cheers!
    Last edited by Francisco Escobar; 22 Nov 2014, 16:16.

  • #2
    If your variable unemployment is a numeric variable, you could do it this way:

    Code:
    levelsof period, local(periods)
    foreach p of local periods {
        egen unemployment`p' = max(cond(period == `p', unemployment, .))
    }

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      If your variable unemployment is a numeric variable, you could do it this way:

      Code:
      levelsof period, local(periods)
      foreach p of local periods {
      egen unemployment`p' = max(cond(period == `p', unemployment, .))
      }

      Thank you! Could you explain what the command is doing?

      It worked really well, thanks again

      Comment


      • #4
        That and other standard tricks are covered in http://www.stata-journal.com/sjpdf.h...iclenum=dm0055

        escobar090: Please our strong preference here for full real names. For more, see the FAQ Advice.

        Comment

        Working...
        X