Announcement

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

  • How to convert NA to 0?

    Dear statalist,

    I try to analyze democratization episode, which I differentiate into the beginning of an episode and the end of an episode. In my example (see below), I would like to dichotomize NA and the beginning of an episode (years), where NA should be 0 and e.g. 1977 should be 1. Moreover I would like to create a variable, where the current episode is not taken into account. It means that only the first year should be a 1. The other years of the episode should be missings.

    In Mexico, for example, the episode began in 1977 (until 2001). I want the year 1977 (row 3) to count as 1, row 4 to 22 as missings and all the NA to count as 0 (same with Suriname and Ghana).

    1 "Mexico" "NA"
    2 "Mexico" "NA"
    3 "Mexico" "1977"
    4 "Mexico" "1977"
    5 "Mexico" "1977"
    6 "Mexico" "1977"
    7 "Mexico" "1977"
    8 "Mexico" "1977"
    9 "Mexico" "1977"
    10 "Mexico" "1977"
    11 "Mexico" "1977"
    12 "Mexico" "1977"
    13 "Mexico" "1977"
    14 "Mexico" "1977"
    15 "Mexico" "1977"
    16 "Mexico" "1977"
    17 "Mexico" "1977"
    18 "Mexico" "1977"
    19 "Mexico" "1977"
    20 "Mexico" "1977"
    21 "Mexico" "1977"
    22 "Mexico" "1977"
    23 "Suriname" "NA"
    24 "Suriname" "NA"
    25 "Suriname" "NA"
    26 "Suriname" "NA"
    27 "Suriname" "NA"
    28 "Suriname" "NA"
    29 "Suriname" "1987"
    30 "Suriname" "1987"
    31 "Suriname" "1992"
    32 "Ghana" "1946"
    33 "Ghana" "1946"
    34 "Ghana" "NA"
    35 "Ghana" "NA"
    36 "Ghana" "NA"
    37 "Ghana" "NA"
    38 "Ghana" "1957"
    39 "Ghana" "1957"
    40 "Ghana" "NA"
    41 "Ghana" "NA"
    Last edited by Thorben Velner; 06 Dec 2021, 08:21.

  • #2
    Really the same with Suriname? Not 0, 1, 2?

    Based on what you ask for

    Code:
    gen wanted = whatever != "NA"
    assigns 0 to NA and 1 to other values. Here for
    Code:
    whatever
    you need to substitute the name of the variable in question.

    If this doesn't answer enough, please come back with an example of the data using dataex. We mean what we say in https://www.statalist.org/forums/help#stata

    Comment


    • #3
      Dear Nick,
      thank you very much for your answer.

      I specified my concern after my original post:
      I only want the year, where the episode began (e.g. 1977) to count as 1. I want the other years of the episode (e.g. 1978 - 2001) to count as a missing, because I only need the start year of the episode. The years before or after the episode should count as 0.

      For the example above I used -dataex-, but only copied 36 rows (out of 100), because I thought it would be enough. I also added the row numbers by hand.
      Here“s the full example using -dataex-:


      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "NA"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Mexico" "1977"
      "Suriname" "NA"
      "Suriname" "NA"
      "Suriname" "NA"
      "Suriname" "NA"
      "Suriname" "NA"
      "Suriname" "NA"
      "Suriname" "1987"
      "Suriname" "1987"
      "Suriname" "1992"
      "Ghana" "1946"
      "Ghana" "1946"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "1957"
      "Ghana" "1957"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "1969"
      "Ghana" "1969"
      "Ghana" "1969"
      "Ghana" "1969"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "1979"
      "Ghana" "1979"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "NA"
      "Ghana" "1992"
      "Ghana" "1992"
      "Ghana" "1992"
      "Ghana" "1992"
      "Ghana" "1992"

      Comment


      • #4
        You did say that: my misreading. The specification is in #1.

        At the same time, #3 still isn't full dataex output as it doesn't even name the variables.

        I assume somewhere that you have variables with names something like country and year from which a code sketch is

        Code:
        egen whenfirst  = min(cond(whatever != "NA", year, .)), by(country)
        gen wanted =  1 if year == whenfirst 
        replace wanted = 0 if whatever == "NA"


        See also https://www.stata.com/support/faqs/d...t-occurrences/ -- as this is an FAQ.
        Last edited by Nick Cox; 06 Dec 2021, 11:11.

        Comment

        Working...
        X