Announcement

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

  • How to replace the age and exit at a certain age

    Dear All,

    Is there a way I can replace all age with the current age+1 till 2040 at one go and also for a person once the age reaches the age 60 then it should exit and in the exit variable mention the year of exit. I know I can do it one by one but it will take a lot of time.

    For example lets take 2020 to 2024,

    age_2020 age 2021 age_2022 age_2023 age_2024 exit
    22 23 24 25 26 .
    58 59 60 . 2022


    Here is an example of my dataset (100 out of 30000 of my records)

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte age_2020 float(age_2021 age_2022 age_2023 age_2024 age_2025 age_2026 age_2027 age_2028 age_2029 age_2030 age_2031 age_2032 age_2033 age_2034 age_2035 age_2036 age_2037 age_2038 age_2039 age_2040 exit)
    27 . . . . . . . . . . . . . . . . . . . . .
    33 . . . . . . . . . . . . . . . . . . . . .
    24 . . . . . . . . . . . . . . . . . . . . .
    49 . . . . . . . . . . . . . . . . . . . . .
    29 . . . . . . . . . . . . . . . . . . . . .
    36 . . . . . . . . . . . . . . . . . . . . .
    43 . . . . . . . . . . . . . . . . . . . . .
    30 . . . . . . . . . . . . . . . . . . . . .
    25 . . . . . . . . . . . . . . . . . . . . .
    34 . . . . . . . . . . . . . . . . . . . . .
    30 . . . . . . . . . . . . . . . . . . . . .
    30 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    29 . . . . . . . . . . . . . . . . . . . . .
    35 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    29 . . . . . . . . . . . . . . . . . . . . .
    29 . . . . . . . . . . . . . . . . . . . . .
    31 . . . . . . . . . . . . . . . . . . . . .
    33 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    25 . . . . . . . . . . . . . . . . . . . . .
    32 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    40 . . . . . . . . . . . . . . . . . . . . .
    35 . . . . . . . . . . . . . . . . . . . . .
    31 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    30 . . . . . . . . . . . . . . . . . . . . .
    32 . . . . . . . . . . . . . . . . . . . . .
    31 . . . . . . . . . . . . . . . . . . . . .
    27 . . . . . . . . . . . . . . . . . . . . .
    26 . . . . . . . . . . . . . . . . . . . . .
    31 . . . . . . . . . . . . . . . . . . . . .
    25 . . . . . . . . . . . . . . . . . . . . .
    34 . . . . . . . . . . . . . . . . . . . . .
    31 . . . . . . . . . . . . . . . . . . . . .
    36 . . . . . . . . . . . . . . . . . . . . .
    25 . . . . . . . . . . . . . . . . . . . . .
    25 . . . . . . . . . . . . . . . . . . . . .
    29 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    29 . . . . . . . . . . . . . . . . . . . . .
    31 . . . . . . . . . . . . . . . . . . . . .
    27 . . . . . . . . . . . . . . . . . . . . .
    30 . . . . . . . . . . . . . . . . . . . . .
    29 . . . . . . . . . . . . . . . . . . . . .
    49 . . . . . . . . . . . . . . . . . . . . .
    27 . . . . . . . . . . . . . . . . . . . . .
    32 . . . . . . . . . . . . . . . . . . . . .
    32 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    32 . . . . . . . . . . . . . . . . . . . . .
    44 . . . . . . . . . . . . . . . . . . . . .
    47 . . . . . . . . . . . . . . . . . . . . .
    40 . . . . . . . . . . . . . . . . . . . . .
    36 . . . . . . . . . . . . . . . . . . . . .
    35 . . . . . . . . . . . . . . . . . . . . .
    35 . . . . . . . . . . . . . . . . . . . . .
    52 . . . . . . . . . . . . . . . . . . . . .
    30 . . . . . . . . . . . . . . . . . . . . .
    47 . . . . . . . . . . . . . . . . . . . . .
    45 . . . . . . . . . . . . . . . . . . . . .
    45 . . . . . . . . . . . . . . . . . . . . .
    41 . . . . . . . . . . . . . . . . . . . . .
    41 . . . . . . . . . . . . . . . . . . . . .
    49 . . . . . . . . . . . . . . . . . . . . .
    46 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    26 . . . . . . . . . . . . . . . . . . . . .
    26 . . . . . . . . . . . . . . . . . . . . .
    27 . . . . . . . . . . . . . . . . . . . . .
    29 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    31 . . . . . . . . . . . . . . . . . . . . .
    41 . . . . . . . . . . . . . . . . . . . . .
    44 . . . . . . . . . . . . . . . . . . . . .
    30 . . . . . . . . . . . . . . . . . . . . .
    56 . . . . . . . . . . . . . . . . . . . . .
    37 . . . . . . . . . . . . . . . . . . . . .
    34 . . . . . . . . . . . . . . . . . . . . .
    41 . . . . . . . . . . . . . . . . . . . . .
    37 . . . . . . . . . . . . . . . . . . . . .
    31 . . . . . . . . . . . . . . . . . . . . .
    32 . . . . . . . . . . . . . . . . . . . . .
    36 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    29 . . . . . . . . . . . . . . . . . . . . .
    31 . . . . . . . . . . . . . . . . . . . . .
    33 . . . . . . . . . . . . . . . . . . . . .
    49 . . . . . . . . . . . . . . . . . . . . .
    35 . . . . . . . . . . . . . . . . . . . . .
    44 . . . . . . . . . . . . . . . . . . . . .
    34 . . . . . . . . . . . . . . . . . . . . .
    41 . . . . . . . . . . . . . . . . . . . . .
    28 . . . . . . . . . . . . . . . . . . . . .
    29 . . . . . . . . . . . . . . . . . . . . .
    end


  • #2
    Sure, as this shows


    Code:
    forval j = 21/40 {
         replace age_20`j' = age_2020 + `j' - 20
         replace exit = 20`j' if age_20`j' == 60 
         replace age_20`j' = . if age_20`j'' >= 61 
    }
    but what's the point of these 20 extra variables in 30000 observations? There is no new information in any of these variables.
    Last edited by Nick Cox; 13 Aug 2021, 03:45.

    Comment


    • #3
      Dear Nick Nick Cox

      Thank you for your answer (age worked) I am making a projection so I did not included the other variables but will you be also able to help me on how I can put a condition where once a person attains the age of 50 (I said 60 but in the example I have provided above no one turns 60) so once a person turns 50 the age variables after that should be left empty. For example if the person turns 50 at year 2030 then from 2030 to 2040 the age_2030 to age 2040 will be missing and the last variable "exit" should capture the exit year (in this example it will be 2040 as he turned 50 in the year 2040)

      Thank you in advance

      Comment


      • #4
        Dear Nick

        forval j = 21/40 {
        replace age_20`j' = age_2020 + `j' - 20
        replace exit = 20`j' if age_20`j' == 60

        }

        This one works but how can i modify the code wherein for those where the age of 60 is attained from that year onward the age should be replaced by missing.

        Thank you
        Last edited by andrew roder; 13 Aug 2021, 04:06.

        Comment


        • #5
          The code answering #4 was already included in #2.

          The answer to #3 is to replace 60 by 50 wherever it appears in the code.

          Comment


          • #6
            Great Thanks you so much

            Comment

            Working...
            X