Announcement

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

  • Running the exact same command n times

    My data is time series individual data. For some personens in some years there are missing information in time invariant variables. The is an artifact of the data, so it is ok to substitute the missing data with data from previous/following years, i.e., it is ok to assume that the sex of a person is constant over time. My data looks something like this:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(pnr year male)
    1 1 0
    1 2 0
    1 3 0
    1 4 0
    1 5 0
    1 6 0
    2 1 1
    2 2 1
    2 3 1
    2 4 .
    2 5 .
    2 6 1
    3 1 .
    3 2 .
    3 3 .
    3 4 .
    3 5 1
    3 6 1
    end
    Where 'pnr' is the individual id, 'year' denotes the year, and 'male' denotes the sex of the person. For persons where there are information on the time invariant variables in years preceeding the years with missing information (person 2) I can fill out the data with:

    Code:
    replace male=male[_n-1] if male==. & pnr==pnr[_n-1]
    However, for the persons (person 3) where there are only data in the years folowing the years with missing information, I have to run the command for every year where there is missing information:

    Code:
    replace male=male[_n+1] if male==. & pnr==pnr[_n+1]
    replace male=male[_n+1] if male==. & pnr==pnr[_n+1]
    replace male=male[_n+1] if male==. & pnr==pnr[_n+1]
    replace male=male[_n+1] if male==. & pnr==pnr[_n+1]
    This is tedious to do in my real data, so how do I tell Stata to run the exact same command n times?

  • #2
    Perhaps using -egen- will do the job more efficiently?

    Code:
    tempvar male
    egen `male' = max(male), by(pnr)
    replace male = `male'
    drop `male'

    Comment


    • #3
      A one-line solution

      Code:
      bysort pnr (male): replace male = male[1]

      Comment


      • #4
        See also the FAQ https://www.stata.com/support/faqs/d...issing-values/ for the device of reversing time.

        Comment


        • #5
          Thanks all for your solutions, . Maybe there is always a more efficient solution (as here), but for the intrinsic value of learning Stata I am still interested in knowing whether there is a way to make Stata run the exact same command n times (other than typing it out n times)?

          Comment


          • #6
            Of course, there are for loops, which is a structure present in most programming languages. Try

            Code:
            help forvalues
            and

            Code:
            help foreach

            Comment


            • #7
              So I figured one way to do it would be:

              Code:
              forval x =1/n{
              command
              }
              In my case:

              Code:
              forval x =1/5{
              replace male=male[_n+1] if male==. & pnr==pnr[_n+1]
              }

              Comment


              • #8
                Since you give an example in #7, what is it you hope to accomplish? A by statement will be more efficient in speed and written code.

                Comment


                • #9
                  Originally posted by Leonardo Guizzetti View Post
                  Since you give an example in #7, what is it you hope to accomplish?
                  What I hope to accomplish is stated in #1 (solving the problem explained) and #5 (a generic method to execute a command n times)

                  Comment

                  Working...
                  X