Announcement

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

  • Applying value label do file into collapse/relabel code

    Hello all: I am trying tweak this code here to make it copy variables label AND copy the values labels from the value label do file I created. How do I sneak in the code to apply the value labels to the collapsed file? The variable label part works well.

    label save using txlabel-v1.do, replace

    // COPY VARIABLE LABELS PRECOLLAPSE

    foreach v of var * {
    local l`v' : variable label `v'
    if `"`l`v''"' == "" {
    local l`v' "`v'"
    }
    }
    // Collapsing/relabel row level tx data for final merges for consumption by 05_allmerge-v1.do later.
    {
    preserve
    collapse (firstnm) clltx1-rttxn (max) cllchem-txnonlymphoma (firstnm) rttx1cat-clltx1cat rtx1-tox, by(studyid) // Reuse copied variable labels
    foreach v of var * {
    label var `v' `"`l`v''"'
    }
    save ".\rtproject\rawdata\05.1_txcollapsed-v1.dta", replace
    restore
    }


    save ".\rtproject\rawdata\05_txcleaned-v1.dta", replace // Cleaned Tx-response file

    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int studyid byte rtx1
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 2
    1 2
    1 2
    1 2
    1 2
    1 2
    1 2
    1 2
    2 .
    2 .
    2 .
    2 .
    2 .
    2 .
    2 .
    2 .
    2 .
    2 .
    2 .
    2 .
    2 .
    2 .
    2 .
    2 .
    3 .
    3 .
    3 .
    3 .
    3 .
    3 .
    3 .
    3 .
    3 6
    3 6
    3 6
    3 6
    3 6
    3 6
    3 6
    3 6
    4 .
    4 .
    4 .
    4 .
    4 .
    4 .
    4 .
    4 .
    4 6
    4 6
    4 6
    4 6
    4 6
    4 6
    4 6
    4 6
    5 .
    5 .
    5 .
    5 .
    5 .
    5 .
    5 .
    5 .
    5 .
    5 .
    5 .
    5 .
    5 .
    5 .
    5 .
    5 .
    6 .
    6 .
    6 .
    6 .
    6 .
    6 .
    6 .
    6 .
    6 2
    6 2
    6 2
    6 2
    6 2
    6 2
    6 2
    6 2
    7 .
    7 .
    7 .
    7 .
    end
    label values rtx1 rtx1
    label def rtx1 2 "BTKi/BCL2i", modify
    label def rtx1 6 "Chemo-R-CHOP/CHOP-like", modify
    [/CODE]

  • #2
    Add the following line after the -collapse- command:
    Code:
    run txlabel-v1 //OR -do- IF YOU WANT TO SEE THE CODE ECHOED

    Comment


    • #3
      Had no idea about a run command. Will try that.

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        Add the following line after the -collapse- command:
        Code:
        run txlabel-v1 //OR -do- IF YOU WANT TO SEE THE CODE ECHOED
        Clyde Schechter : I tried the -run- command as you suggested above to reuse value labels after collapse, but the value labels don't show up. There is no error either and the command runs.
        I can -dataex- if that would help troubleshoot. I have the last bit of the do file down here.


        label save using txlabel-v1.do, replace

        // COPY VARIABLE LABELS PRECOLLAPSE

        foreach v of var * {
        local l`v' : variable label `v'
        if `"`l`v''"' == "" {
        local l`v' "`v'"
        }
        }
        // Collapsing/relabel row level tx data for final merges for consumption by 05_allmerge-v1.do later.
        {
        preserve
        collapse (firstnm) clltx1-rttxn (max) cllchem-txnonlymphoma (firstnm) rttx1cat-clltx1cat rtx1-tox, by(studyid) // Reuse copied variable labels
        run txlabel-v1
        foreach v of var * {
        label var `v' `"`l`v''"'
        }

        save ".\rtproject\rawdata\05.1_txcollapsed-v2.dta", replace
        restore
        }


        save ".\rtproject\rawdata\05_txcleaned-v2.dta", replace // Cleaned Tx-response file

        Comment


        • #5
          Sorry. I forgot to mention that after you -run txlabel-v1-, you then have to apply the labels to the corresponding variables using -label values...- commands. That, in turn, probably should be done in a loop given the number of variables involved. So the code would end up looking like this:

          Code:
          label save using txlabel-v1.do, replace
          
          // COPY VARIABLE LABELS PRECOLLAPSE
          
          foreach v of var * {
              local l`v' : variable label `v'
              local vl`v': value label `v'
              if `"`l`v''"' == "" {
                  local l`v' "`v'"
              }
          }
          // Collapsing/relabel row level tx data for final merges for consumption by 05_allmerge-v1.do later.
          
          preserve
          collapse (firstnm) clltx1-rttxn (max) cllchem-txnonlymphoma (firstnm) rttx1cat-clltx1cat rtx1-tox, by(studyid) // Reuse copied variable labels
          run txlabel-v1
          foreach v of var * {
              label var `v' `"`l`v''"'
              label values `v' `vl`v''
          }
          
          // ETC....

          Comment


          • #6
            Of course. This makes total sense now and helps me wrap my mind around what the loop does. Thanks much.

            Comment


            • #7
              Originally posted by Clyde Schechter View Post
              Sorry. I forgot to mention that after you -run txlabel-v1-, you then have to apply the labels to the corresponding variables using -label values...- commands. That, in turn, probably should be done in a loop given the number of variables involved. So the code would end up looking like this:

              Code:
              label save using txlabel-v1.do, replace
              
              // COPY VARIABLE LABELS PRECOLLAPSE
              
              foreach v of var * {
              local l`v' : variable label `v'
               local vl`v': value label `v'
              if `"`l`v''"' == "" {
              local l`v' "`v'"
              }
              }
              // Collapsing/relabel row level tx data for final merges for consumption by 05_allmerge-v1.do later.
              
              preserve
              collapse (firstnm) clltx1-rttxn (max) cllchem-txnonlymphoma (firstnm) rttx1cat-clltx1cat rtx1-tox, by(studyid) // Reuse copied variable labels
              run txlabel-v1
              foreach v of var * {
              label var `v' `"`l`v''"'
               label values `v' `vl`v''
              }
              
              // ETC....
              This gave an r(181) error saying it ''cannot attach value labels to strings.' I can see that I logically need to add an an -if- statement within the loop to restrict value labeling to non-strings. -ds, has(vall)- or perhaps -ds, not(type string)-
              Perhaps the value labels need to be a separate loop? I tried it but now it is some r(100) error. Maybe I am not capturing variables with varlabels appropriately.

              // COPY VARIABLE LABELS PRECOLLAPSE

              foreach v of var * {
              local l`v' : variable label `v'
              local vl`v': value label `v'
              if `"`l`v''"' == "" {
              local l`v' "`v'"
              }
              }
              // Collapsing/relabel-re-value label row level tx data for final merges for consumption by 05_allmerge-v1.do later.

              preserve
              collapse (firstnm) clltx1-rttxn (max) cllchem-txnonlymphoma (firstnm) rttx1cat-clltx1cat rtx1-tox, by(studyid) // Reuse copied variable labels

              foreach v of var * {
              label var `v' `"`l`v''"'

              }

              ds, has(vall)
              foreach v of `r(varlist)'{
              label values `v' `vl`v''
              }



              save ".\rtproject\rawdata\05.1_txcollapsed-v2.dta", replace
              restore

              Comment


              • #8
                The syntax error you are getting is from -foreach v of `r(varlist)'{-.It should be -foreach v of varlist `r(varlist)' {-.

                Comment


                • #9
                  Originally posted by Clyde Schechter View Post
                  The syntax error you are getting is from -foreach v of `r(varlist)'{-.It should be -foreach v of varlist `r(varlist)' {-.
                  I changed that but now, the loop halts with and r(100) error as below:

                  // upper part of code....

                  . foreach v of varlist `r(varlist)'{
                  2. label values `v' `vl`v''
                  3. }
                  varlist required
                  r(100);

                  Comment


                  • #10
                    Right, I didn't see that coming. The problem now is with -ds, has(vall)-, because at that point none of the variables has a value label. In fact you're just trying to put value labels on them. So change that to -ds, has(type numeric)-. Then I think it will work correctly. If there is a numeric variable that never had a value label in the first place, local vl`v' will not exist, and the -label values- command will be understood by Stata to mean that that variable `v' should not have a value label, which is correct.

                    Comment


                    • #11
                      Yes. That worked now Thanks much. You have probably contributed much more to my study than some of my coauthors.

                      Comment

                      Working...
                      X