Announcement

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

  • Dummy variable using abbreviation

    Hi all,

    I'm trying to set up a dummy variable for whether an individual grows crops or not. However I have 200 variables in the format of YES = 1 and NO = 0 [with a lot of missing values]. Figure 1 shows the data style, I created a varlist for all variables starting with ag4a_06

    Code:
    vl create harvested = (ag4a_06*)
    Then with this I'm trying to create the dummy with this code:

    Code:
    gen harvest = 1
    replace harvest = 0 if $harvested = 2
    However I get the error "Invalid ag4a_06M1_100" which is only the second ag4a_06 variable?

    The variable is broken down as such:
    - ag4a_06 refers to the question number [Did you harvest any of [CROP] on this plot?] so is either missing, 1 or 2.
    - MX refers to the plot number
    - _Y refers to the crop id

    Figure 1:
    Click image for larger version

Name:	Screenshot 2022-02-16 at 15.41.42.png
Views:	1
Size:	38.8 KB
ID:	1650425

  • #2
    This is the data set example:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str16 y2_hhid byte(ag4a_06M10_11 ag4a_06M1_100 ag4a_06M1_101 ag4a_06M1_102 ag4a_06M1_11 ag4a_06M1_12 ag4a_06M1_13 ag4a_06M1_14 ag4a_06M1_15)
    "0101014002017101" . . . . . . 1 1 .
    "0101014002017101" . . . . . . 1 1 .
    "0101014002017101" . . . . . . 1 1 .
    "0101014002028401" . . . . 1 . . . .
    "0101014002028401" . . . . 1 . . . .
    "0101014002028401" . . . . 1 . . . .
    "0101014002028401" . . . . 1 . . . .
    "0101014002029701" . . . . 1 . . . .
    "0101014002029701" . . . . 1 . . . .
    "0101014002029701" . . . . 1 . . . .
    "0101014002029701" . . . . 1 . . . .
    "0101014002029701" . . . . 1 . . . .
    "0101014002029701" . . . . 1 . . . .
    "0101014002029701" . . . . 1 . . . .
    "0101014002029701" . . . . 1 . . . .
    "0101014002029701" . . . . 1 . . . .
    "0101014002029701" . . . . 1 . . . .
    "0101014002040901" . . . . . . . 1 .
    "0101014002040901" . . . . . . . 1 .
    "0101014002040901" . . . . . . . 1 .
    "0101014002040901" . . . . . . . 1 .
    "0101014002040901" . . . . . . . 1 .
    "0101014002040901" . . . . . . . 1 .
    "0101014002047101" . . . . . . 1 1 .
    "0101014002047101" . . . . . . 1 1 .
    "0101014002047101" . . . . . . 1 1 .
    "0101014002047101" . . . . . . 1 1 .
    "0101014002047101" . . . . . . 1 1 .
    "0101014002055101" . . . . . . . 1 .
    "0101014002055101" . . . . . . . 1 .
    "0101014002055101" . . . . . . . 1 .
    "0101014002055101" . . . . . . . 1 .
    "0101014002055101" . . . . . . . 1 .
    "0101014002076201" . . . . 1 . . . .
    "0101014002076201" . . . . 1 . . . .
    "0101014002076201" . . . . 1 . . . .
    "0101014002076201" . . . . 1 . . . .
    "0102003003000401" . . . . 1 . . . .
    "0102003003000401" . . . . 1 . . . .
    "0102003003002201" . . . . . . 1 . .
    "0102003003002201" . . . . . . 1 . .
    "0102003003002201" . . . . . . 1 . .
    "0102003003002201" . . . . . . 1 . .
    "0102003003002201" . . . . . . 1 . .
    "0102003003002201" . . . . . . 1 . .
    "0102003003002201" . . . . . . 1 . .
    "0102003003002201" . . . . . . 1 . .
    "0102003003002201" . . . . . . 1 . .
    "0102003003002201" . . . . . . 1 . .
    "0102003003002201" . . . . . . 1 . .
    "0102003003017401" . . . . 1 . . . .
    "0102003003017401" . . . . 1 . . . .
    "0102003003017401" . . . . 1 . . . .
    "0102003003017401" . . . . 1 . . . .
    "0102003003017401" . . . . 1 . . . .
    "0102003003017401" . . . . 1 . . . .
    "0102003003017401" . . . . 1 . . . .
    "0102003003020001" . . . . . . . . .
    "0102003003020001" . . . . . . . . .
    "0102003003020001" . . . . . . . . .
    "0102003003020001" . . . . . . . . .
    "0102003003020001" . . . . . . . . .
    "0102003003020001" . . . . . . . . .
    "0102003003043001" . . . . 1 . . . .
    "0102003003043001" . . . . 1 . . . .
    "0102003003043001" . . . . 1 . . . .
    "0102003003043001" . . . . 1 . . . .
    "0102003003043001" . . . . 1 . . . .
    "0102003003043001" . . . . 1 . . . .
    "0102003003043001" . . . . 1 . . . .
    "0102003003043001" . . . . 1 . . . .
    "0102003003047901" . . . . 1 . . . .
    "0102003003047901" . . . . 1 . . . .
    "0102003003047901" . . . . 1 . . . .
    "0102003003047901" . . . . 1 . . . .
    "0102003003047901" . . . . 1 . . . .
    "0102017003000101" . . . . . . 1 . .
    "0102017003000101" . . . . . . 1 . .
    "0102017003000101" . . . . . . 1 . .
    "0102017003000101" . . . . . . 1 . .
    "0102017003000101" . . . . . . 1 . .
    "0102017003000101" . . . . . . 1 . .
    "0102017003000101" . . . . . . 1 . .
    "0102017003000101" . . . . . . 1 . .
    "0102017003000101" . . . . . . 1 . .
    "0102017003000104" . . . . . . 1 . .
    "0102017003000104" . . . . . . 1 . .
    "0102017003000104" . . . . . . 1 . .
    "0102017003001701" . . . . 1 . . . .
    "0102017003001701" . . . . 1 . . . .
    "0102017003001701" . . . . 1 . . . .
    "0102017003001701" . . . . 1 . . . .
    "0102017003002201" . . . . . . 1 . .
    "0102017003002201" . . . . . . 1 . .
    "0102017003002201" . . . . . . 1 . .
    "0102017003002201" . . . . . . 1 . .
    "0102017003002201" . . . . . . 1 . .
    "0102017003002201" . . . . . . 1 . .
    "0102017003002201" . . . . . . 1 . .
    "0102017003004801" . . . . . . 1 . .
    end
    label values ag4a_06M10_11 ag4a_06
    label values ag4a_06M1_100 ag4a_06
    label values ag4a_06M1_101 ag4a_06
    label values ag4a_06M1_102 ag4a_06
    label values ag4a_06M1_11 ag4a_06
    label values ag4a_06M1_12 ag4a_06
    label values ag4a_06M1_13 ag4a_06
    label values ag4a_06M1_14 ag4a_06
    label values ag4a_06M1_15 ag4a_06
    label def ag4a_06 1 "YES", modify

    Comment


    • #3
      Testing for equality requires == not = and you can't put a variable list on either side of an equality.

      Your results contradict the description YES = 1 and NO = 0

      Code:
      egen harvest = anymatch(ag4a_06*), values(2)
      may help. See also the help for the any* functions in help egen.

      Comment


      • #4
        Thank you for your reply Nick, and sorry for the contradiction had been a long week. This worked, just changing to
        Code:
        values(1)
        got me where I wanted to be.

        Thanks again

        Comment

        Working...
        X