Announcement

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

  • Problem with replace if

    Hello,

    I am a new Stata user and I'm currently using Stata 15.1. I am trying to replace some of the values in my data. I have two variables that indicate the lowest grade offered (gslo) in a school, and the highest grade offered (gshi) in a school. I also have about 260 variables that simultaneously indicate a student's ethnicity, grade and gender i.e. ampkm is American Indian Pre-K male.

    Gslo and gshi are categorical variables that include observations from "pk" to "12". I need to replace observations for the ethnicity variables based on the highest and lowest grade offered. For example, if a school offers 5th through 12th grade, and ampkm has a value of 0, it should be replaced with .n, as in not applicable.

    I have tried:
    Code:
    sort gslo gshi
    Code:
     replace ampkm =.n if gslo == "05" & gshi == "12"
    However, I have found that this will only replace observations if the lowest grade is 5th and the highest grade is 12th, rather than 5th through 12th grade.

    How may I go about doing this so I can replace the variables based on a range?

    Thanks in advance,

    Brianna

  • #2
    First, you also need an &ampkm == 0 tacked on to your command for it to work as you say you want.

    But I'm confused. What does "5th through 12th grade" mean if not that the lowest grade is 5 and the highest is 12?

    Comment


    • #3
      Dear Clyde,

      Thank you for the response.

      Sorry for the lack of clarification. The data has many schools where some have gslo == “05” and gshi ==“12”. But, it also has schools where gslo == “09” and gshi == “12” or gslo ==“kg” and gshi =“12”.

      When I ran this code it seemed as though it would only change observations for schools that just offered 5th through 12th, rather than all schools that offer any grade between 5 and 12.

      Comment


      • #4
        OK. That's different. I think that sooner or later you will need to change gslo and gshi to numeric variables, because if you need to sort things, "pk" does not sort before "01." Now would be as good a time as any. I'll assume that in addition to pk there is also a k.

        Code:
        foreach v of varlist gslo gshi {
            replace `v' = "0" if `v' == "k"
            replace `v' = "-1" if `v' == "pk"
            destring `v', replace
        }
        
        
        replace ampkm = .n if ampkm == 0 & ///
            (inrange(gslo, 5, 12) | inrange(gshi, 5, 12))
        Note: Not tested.

        Comment


        • #5
          Thank you very much!

          Comment

          Working...
          X