Announcement

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

  • How to write loops in this case

    Dear all,
    Consider the following data:
    clear
    input int index byte age
    1010 26
    1011 41
    1020 28
    1021 46
    1030 32
    1031 44
    1040 27
    1041 28
    2010 49
    2011 45
    2020 25
    2021 29
    2030 25
    2031 29
    2040 49
    2041 48
    3010 28
    3011 48
    3020 43
    3021 28
    3030 43
    3031 48
    3040 35
    3041 48
    4010 36
    4011 37
    4020 40
    4021 45
    4030 30
    4031 49
    4040 49
    4041 33
    5010 40
    5011 43
    5020 38
    5021 46
    5030 49
    5031 27
    5040 46
    5041 49
    6010 24
    6011 23
    6020 36
    6021 49
    6030 35
    6031 47
    6040 37
    6041 46
    7010 43
    7011 27
    7020 48
    7021 30
    7030 47
    7031 39
    7040 43
    7041 28
    8010 40
    8011 40
    8020 30
    8021 45
    8030 45
    8031 30
    8040 35
    8041 38
    9010 38
    9011 44
    9020 44
    9021 49
    9030 45
    9031 32
    9040 28
    9041 44
    10101 31
    10110 38
    10111 44
    10120 37
    10121 35
    10130 20
    10131 39
    10140 40
    10141 29
    10210 36
    10211 49
    10220 25
    10221 32
    10230 15
    10231 42
    10240 35
    10241 22
    end
    I want to generate a series of t variables for all values of index variable. Take values 1010 and 1011 of index variable as an example:
    Code:
        xi i.index
        forval i = 1010/1011 {
            cap gen double t`i' = 0
            cap replace t`i' = (49 - age) if _Iindex_`i'==1
        }
    The code above works but I have to repeat and revise the code many time. Is there a shorter way to obtain what I want? Thank you.
    Last edited by Matthew Williams; 12 Mar 2023, 11:47.

  • #2
    Maybe somebody else understands what you want here and will respond soon. For my part, I have no idea what you're getting at. There are no _Iindex_* variables in your example data: what are they? They look like something that might have come from the archaic -xi- command. What are you actually trying to accomplish here? Perhaps show a smaller example of your data, perhaps 4 or 5 values of index, and then follow-up with what you would like the results for that smaller example to look like.

    Also, do explain what you plan to do with those results once you get them. It looks like you are heading towards a data set with a gazillion variables each of which contains very little information. This approach is usually not a very efficient or effective one in Stata--if we know your longer goals, we might be able to figure out a better way to do it altogether.
    Last edited by Clyde Schechter; 12 Mar 2023, 12:01.

    Comment


    • #3
      Dear Prof. Clyde,

      Thank you for your response and I am sorry for not being clear on the purpose and variables. Let me respond to #2 point-by-point.
      First, you are right, I use "xi i.index" command to generate a list of dummies for each value of index. For example, _Iindex_10101 takes on a value of one if index==1010 and 0 otherwise. Similar logics are applied to other dummies. However, when I read your comments, I realized that I don't need that command, it seems redundant. A revised code could be:
      Code:
          forval i = 1010/1011 {
              cap gen double t`i' = 0
              cap replace t`i' = (49 - age) if index==`i'
          }
      Second, I want to generate a series of age trends across regions, defined as the distance between 49 and the actual age in a certain region. In fact, index contains regional codes (e.g., 1010, 1011 and so on) (sorry again for not defining this variable in #1). Since my econometric specifications reply on a policy that varies by region and rural-urban areas, I need to control for age trends across regions in regressions to avoid potential biases.
      Please post back if things are still unclear to you.

      Thank you.

      Comment


      • #4
        So it sounds like you want to do this:
        Code:
        levelsof index, local(indices)
        foreach i of local indices {
            gen t`i' = cond(index == `i', 49-age, 0)
        }
        That said, I don't grasp why you want a separate such variable for each region. It seems to me that a single variable, -gen t = 49 - age-, would enable you to adjust for deviation from age 49 in each region in any regression. Maybe I am misunderstanding something.

        Comment


        • #5
          Dear Prof. Clyde
          Thank you so much for your wonderful help as usual. I forgot the usefulness of levelsof command. It fits my need perfectly.
          I have just posted a new thread regarding merging two datasets where the household data contains string values in household id and the individual data contains integer values in personal id. The discrepancy in types of values stored in household id and personal id creates difficulties in merging. If you have time, are interested and don't mind, please visit the thread at: https://www.statalist.org/forums/for...r-values-in-id

          Thank you.

          Comment

          Working...
          X