Announcement

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

  • Replace - Replacing too many values

    Hello everybody,

    I have the following problem: II try to replace ethn1_2020=0 if DE_ETHN1!=276 & country==276. Actually this should change about 50 values - but instead 129 are being changed... and I have no clue why. (The two variables are connected in the sense that they both were constructed from 1 multiple choice question.)

    I hope I apply the dataex command correctly:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(DE_ETHN1 DE_ETHN2) float ethn1_2020 int country
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    -2 -2 . 40
    end
    label values DE_ETHN1 DE_ETHN1
    label def DE_ETHN1 -2 "-2. NAP, other countries", modify
    label values DE_ETHN2 DE_ETHN2
    label def DE_ETHN2 -2 "-2. NAP, other countries", modify
    label values ethn1_2020 ethn1_lb
    label values country COUNTRY
    label def COUNTRY 40 "40. AT-Austria", modify
    In case that no, I attach the dofile and shortened dataset.
    Attached Files

  • #2
    Why do you say the replace command should only make 50 changes? it looks to me like there are 129 observations where DE_ETHN1 does not equal 276 & country is equal to 276.

    Code:
    . count if country==276 & DE_ETHN1 != 276
      129
    ​​​​​​​

    Comment


    • #3
      Hello everyone,
      This may not be realted to the psot above. But let me ask from here

      I want to generate a variable time based on another varaiable k22_01. My line of command is as follows

      gen time=.
      replace time=1 if k22_01== [I have to list several values here]

      The challenge is that k22 has values raning from 1 to 200. The values I want to use to generate my new variable time range from 100 to 171. I dread listing the numbers 100 to 171 in that second line of my code (replace ... if k22_01==). Is there a shorter way I can deal with that?

      Thanks

      Comment


      • #4
        There is no need to start with -gen time = .-. I see that all the time here on Statalist. It is a relic from other statistical packages that do not work with logical expressions. All you need is:
        Code:
        gen time = inrange(k22_01, 100, 171)

        Comment

        Working...
        X