Announcement

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

  • Loop foreach help

    Hi everyone. I am trying to fill in missing values in one variable by completing it with values from another one. I am working with observations with often lack the end date, in which case I want to make the end date be the same as the start date. I use this code:

    foreach x of end_year {
    replace end_year = start_year `x' if missing(end_year`x')
    }

    and I am getting an error message saying "invalid 'end_year'). Nevertheless, I have triple checked for extra spaces, but this does not seem to be the issue. What could it be? Thank you.

  • #2
    . dataex start_year end_year

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(start_year end_year)
    1975    .
    1975    .
    1975    .
    1975    .
    1975    .
    1975    .
    1975    .
    1975    .
    1975    .
    1975    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1976    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1977    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1978    .
    1979    .
    1979    .
    1979    .
    1979    .
    1979    .
    1979    .
    1979    .
    1979    .
    1979    .
    1979    .
    1979    .
    1979    .
    1980    .
    1980 1980
    1980    .
    1980    .
    1980    .
    1980    .
    1980    .
    1980    .
    1980    .
    1980    .
    1980    .
    1980    .
    1980    .
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 4153 observations
    Use the count() option to list more

    .

    Comment


    • #3
      no need for looping,
      Code:
      replace end_year = start_year if missing(end_year)

      Comment


      • #4
        Thank you so much that worked!

        Comment

        Working...
        X