Announcement

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

  • How to define missing in value label and apply to multiple variables using this value label?

    Hello!

    New user here. I had 28 string variables with similar data that I encoded to numeric, using a defined value label (SIRorder).

    Code:
     label define SIRorder 1 "S" 2 "I" 3 "R" 4 "NI" 5 "NA" 6 "NULL"
    example:
    Code:
     encode AugNEW , generate(Aug_new2) label(SIRorder)
    I used the value label because not all of the original string variables had each of the data categories, and this gave me more consistency in how the data are encoded. None of the variables have any blank or missing data; the equivalent of missing in the string variables was originally coded as "NULL." In encoding the string variables, I now have a category (#6) for "NULL."

    . label list SIRorder
    SIRorder:
    1 S
    2 I
    3 R
    4 NI
    5 NA
    6 NULL
    7 NB

    How can I indicate that I want #6 to be treated as missing for all variables I generated using the value label SIRorder? My attempt to redefine #6 as missing did not work:

    Code:
     label define SIRorder . == 6, modify
    may not label .
    r(198);

    Should I (could I?) have encoded NULL as missing when I originally encoded the string variables to numeric?

    I could go through each of the 28 new variables and replace #6 with missing, but that seems like a lot of manual work. I may also ultimately want to make SIRorder categories #4, #5, and #7 also be missing (.), so understanding how best to do this for #6 and apply across all 28 variables using the SIRorder value label will potentially be useful down the road.

    Thanks in advance for your patience and help!
    Edie Marshall

  • #2
    I had never tried to include a missing value in a value label used for encode, so I had to try it out to see if it works. It took about 30 sec. to try it. Nothing wrong with trying something easy and seeing if it works. You should try it too and find out what Stata does. You do need to know, though, that only the extended missing values of .a .b, ... can be labeled in Stata.

    Given where you are now, you might also choose -recode- (a command surprisingly often ignored), which will do what you want. See -help recode-
    Code:
    recode YourListOfNewVariables (6 = .a)
    -mvdecode- also will work.

    -help missing- leads to some useful commands and information. My biggest suggestion to new users is to try -help- or -search- with some key words. Such forays into the documentation don't always easily lead to something comprehensible or useful, but often it does.

    Comment


    • #3
      Thanks! I tried -help label- but not -help missing-. I guess I should have thought of it from the recode perspective but was thinking of just trying to fix the value label itself, which obviously didn't work. Thanks again!

      Comment


      • #4
        Adding to Mike's suggestion, you can get the list of variables that have a value label attached (in the current label language) from ds.

        Code:
        ds , has(vallabel SIRorder)
        recode `r(varlist)' ...
        Although it is always a good idea to know how to do things with the tools that official Stata provides, I would also like to point to elabel (SSC). One problem with recode is that you will lose the value labels (text) associated with the integer values. You could re-type the labels, but that seems a bit cumbersome. elabel recodes integer values in value labels, preserving the associated text. Assuming you would decide to recode 4, 5, and 6 to (extended) missing values, .a, .b, and .c, you would

        Code:
        elabel recode SIRorder (4/6 = .a/.c) // <- recode the value label
        return list // <- just for demonstration
        local rules `r(rules)' // <- copy the recoding rules
        ds , has(vallabel SIRorder) // <- get variables that have SIRorder attached
        recode `r(varlist)' `rules' // <- recode all variables accordingly
        Best
        Daniel

        Comment

        Working...
        X