Announcement

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

  • One unique value label for several variables: how changing that automatically ?

    Hi everyone,

    I have a big dataset which i have for hundreds variables an unique label value.
    Here is an example of my export data:
    Code:
    label define allvalue 1 "t1" 2 "t2" ... 10000 "t10000"
    label value var1 allvalue
    label value var2 allvalue
    ...
    label value var300 allvalue
    I want each variables to have their own label using "allvalue" label but just keeping their modalities.

    For example, i want:
    Code:
    label define var1 1 "t1" 2 "t2"
    label value var1 var1
    label define var2 3 "t3" 200 "t200" 250 "t250"
    label value var2 var2
    ...
    I cant do that manually because i have hundreds variables and thousands different
    values, i don't know if it possible to automate this in a function (i have not enouth knowledge to do that myself).

    Help would be quite welcome .
    Last edited by Vincent Spaces; 11 Mar 2020, 08:33.

  • #2
    Code:
    foreach var of varlist *{
           label values `var' allvalue
    }
    I want each variables to have their own label using "allvalue" label but just keeping their modalities.

    For example, i want:
    Code:
    label define var1 1 "t1" 2 "t2"
    label value var1 var1
    label define var2 3 "t3" 200 "t200" 250 "t250"
    label value var2 var2 ...
    You can create the value labels by looping.

    Code:
    local allvalue ""
    forval i=1/250{
    local allvalue "`allvalue' `i' "t`i'""
    }
    Res.:

    Code:
    . di `"`allvalue'"'
     1 "t1" 2 "t2" 3 "t3" 4 "t4" 5 "t5" 6 "t6" 7 "t7" 8 "t8" 9 "t9" 10 "t10" 11 "t11" 12 "t12" 13 "t13" 14 "t14" 15 "t15" 16 "t16" 17 "t17" 18 "t18" 19 "t19" 20 "t20" 21 "t21" 22 "t22" 23
    > "t23" 24 "t24" 25 "t25" 26 "t26" 27 "t27" 28 "t28" 29 "t29" 30 "t30" 31 "t31" 32 "t32" 33 "t33" 34 "t34" 35 "t35" 36 "t36" 37 "t37" 38 "t38" 39 "t39" 40 "t40" 41 "t41" 42 "t42" 43 "t
    > 43" 44 "t44" 45 "t45" 46 "t46" 47 "t47" 48 "t48" 49 "t49" 50 "t50" 51 "t51" 52 "t52" 53 "t53" 54 "t54" 55 "t55" 56 "t56" 57 "t57" 58 "t58" 59 "t59" 60 "t60" 61 "t61" 62 "t62" 63 "t63
    > " 64 "t64" 65 "t65" 66 "t66" 67 "t67" 68 "t68" 69 "t69" 70 "t70" 71 "t71" 72 "t72" 73 "t73" 74 "t74" 75 "t75" 76 "t76" 77 "t77" 78 "t78" 79 "t79" 80 "t80" 81 "t81" 82 "t82" 83 "t83"
    > 84 "t84" 85 "t85" 86 "t86" 87 "t87" 88 "t88" 89 "t89" 90 "t90" 91 "t91" 92 "t92" 93 "t93" 94 "t94" 95 "t95" 96 "t96" 97 "t97" 98 "t98" 99 "t99" 100 "t100" 101 "t101" 102 "t102" 103 "
    > t103" 104 "t104" 105 "t105" 106 "t106" 107 "t107" 108 "t108" 109 "t109" 110 "t110" 111 "t111" 112 "t112" 113 "t113" 114 "t114" 115 "t115" 116 "t116" 117 "t117" 118 "t118" 119 "t119"
    > 120 "t120" 121 "t121" 122 "t122" 123 "t123" 124 "t124" 125 "t125" 126 "t126" 127 "t127" 128 "t128" 129 "t129" 130 "t130" 131 "t131" 132 "t132" 133 "t133" 134 "t134" 135 "t135" 136 "t
    > 136" 137 "t137" 138 "t138" 139 "t139" 140 "t140" 141 "t141" 142 "t142" 143 "t143" 144 "t144" 145 "t145" 146 "t146" 147 "t147" 148 "t148" 149 "t149" 150 "t150" 151 "t151" 152 "t152" 1
    > 53 "t153" 154 "t154" 155 "t155" 156 "t156" 157 "t157" 158 "t158" 159 "t159" 160 "t160" 161 "t161" 162 "t162" 163 "t163" 164 "t164" 165 "t165" 166 "t166" 167 "t167" 168 "t168" 169 "t1
    > 69" 170 "t170" 171 "t171" 172 "t172" 173 "t173" 174 "t174" 175 "t175" 176 "t176" 177 "t177" 178 "t178" 179 "t179" 180 "t180" 181 "t181" 182 "t182" 183 "t183" 184 "t184" 185 "t185" 18
    > 6 "t186" 187 "t187" 188 "t188" 189 "t189" 190 "t190" 191 "t191" 192 "t192" 193 "t193" 194 "t194" 195 "t195" 196 "t196" 197 "t197" 198 "t198" 199 "t199" 200 "t200" 201 "t201" 202 "t20
    > 2" 203 "t203" 204 "t204" 205 "t205" 206 "t206" 207 "t207" 208 "t208" 209 "t209" 210 "t210" 211 "t211" 212 "t212" 213 "t213" 214 "t214" 215 "t215" 216 "t216" 217 "t217" 218 "t218" 219
    >  "t219" 220 "t220" 221 "t221" 222 "t222" 223 "t223" 224 "t224" 225 "t225" 226 "t226" 227 "t227" 228 "t228" 229 "t229" 230 "t230" 231 "t231" 232 "t232" 233 "t233" 234 "t234" 235 "t235
    > " 236 "t236" 237 "t237" 238 "t238" 239 "t239" 240 "t240" 241 "t241" 242 "t242" 243 "t243" 244 "t244" 245 "t245" 246 "t246" 247 "t247" 248 "t248" 249 "t249" 250 "t250"
    Last edited by Andrew Musau; 11 Mar 2020, 09:49.

    Comment


    • #3
      Thank you for your message but i don't understand how your code could help what i'm looking for, maybe because the title is not clear, i don't want to have one unique label value for my variables but creates new labels for each variables from the initial value.
      var1 ... var300 was an example, my variables have real names like sex, nationality ...
      Same for the values "t1" ... "t250" => "Male" "Female" "text"..., so i don't know if i can use loop so easily.

      Comment


      • #4
        I guess there are clues in #2. levelsof may help here, but you will need to loop and loop. The following is not tested, but it may give you some guidance.

        Code:
        foreach var of varlist *{
               local `var'lab ""
               levelsof `var', local(`var')
        foreach num of numlist `var'{
               local `var'lab "``var'lab' `num' "t`num'""
        }
        label values `var' ``var'lab'
        }
        ADDED IN EDIT:

        var1 ... var300 was an example, my variables have real names like sex, nationality ...
        Same for the values "t1" ... "t250" => "Male" "Female" "text"..., so i don't know if i can use loop so easily.
        I may have misunderstood what is wanted, but if you want each value label to include some arbitrary text known to you, I do not see how you can automate this. How should Stata know that 0 for var29 is "female" and 5 for var64 is "very good"? In this case, you have to create the labels one by one. My illustration is for the label including a common word appearing before the value, e.g., adding the letter "t", as in your example.
        Last edited by Andrew Musau; 11 Mar 2020, 11:53.

        Comment


        • #5
          Code:
          I may have misunderstood what is wanted, but if you want each value label to include some arbitrary text known to you, I do not see how you can automate this. How should Stata know that 0 for var29 is "female" and 5 for var64 is "very good"?
          Sorry if i'm not clear about what i want. I try to explain again with a concrete example.

          Click image for larger version

Name:	test.PNG
Views:	1
Size:	3.0 KB
ID:	1540885


          I have this sort of dataset with an unique label value:
          Code:
          label define allvalue 1 "Male" 2 "Female" 3 "French" 4 "England" 8 "German" 6 "Yes" 7 "No"
          label value sex allvalue
          label value nationality allvalue
          label value child allvalue
          And i want to change it into several value labels for each variables like this:
          Code:
          label define sex 1 "Male" 2 "Female"
          label value sex sex
          label define nationality 3 "French" 4 "England" 8 "German"
          label value nationality nationality
          label define child 6 "No" 7 "Yes"
          label value child child
          I can do it manually for my data, but it'll take some weeks if i don't find a way to automate this with my thousands different values.
          Last edited by Vincent Spaces; 11 Mar 2020, 14:30.

          Comment


          • #6
            This is a very odd setup. Where does this dataset come from? What do you want to do with it, once you get those labels?

            Best
            Daniel
            Last edited by daniel klein; 11 Mar 2020, 15:12.

            Comment


            • #7
              Please use dataex in future to present data examples. Refer to FAQ Advice #12. The following assumes that your values lie within the range 1-1000. If you have values outside this range, the code will need to be modified.

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input float(sex nationality child)
              1 3 6
              2 3 6
              2 4 6
              1 4 7
              1 8 7
              2 8 7
              end
              label define allvalue 1 "Male" 2 "Female" 3 "French" 4 "England" 8 "German" 6 "Yes" 7 "No"
              *PRESERVE ORIGINAL DATASET
              preserve
              *GENERATE A VARIABLE WITH LEVELS 1-1000 AND LABEL IT WITH YOUR LABEL
              set obs 1000
              gen extra=_n
              lab values extra allvalue
              *EXTRACT EACH LEVEL
              levelsof extra, local(levels)
                    local lab : val label extra
                    foreach l of local levels {
                    local v`l' : lab `lab' `l'
              }
              *RESTORE ORIGINAL DATASET
              restore
              *GENERATE WANTED LABELS
              foreach var of varlist *{
                    levelsof `var', local(l)
                    foreach lev of local l{
                             local `var' "``var'' `lev' "`v`lev''""
                    }
                    lab def `var'  ``var''
                    label values `var' `var'
              }
              Res.:

              Code:
              . l, sep(10)
              
                   +---------------------------+
                   |    sex   nation~y   child |
                   |---------------------------|
                1. |   Male     French     Yes |
                2. | Female     French     Yes |
                3. | Female    England     Yes |
                4. |   Male    England      No |
                5. |   Male     German      No |
                6. | Female     German      No |
                   +---------------------------+
              
              . lab list
              child:
                         6 Yes
                         7 No
              nationality:
                         3 French
                         4 England
                         8 German
              sex:
                         1 Male
                         2 Female
              allvalue:
                         1 Male
                         2 Female
                         3 French
                         4 England
                         6 Yes
                         7 No
                         8 German
              Last edited by Andrew Musau; 11 Mar 2020, 17:41.

              Comment


              • #8
                As I have mentioned, I have serious doubts that giving Vincent code that does what he wants would actually help him in the longer run. Anyway, here are two ways how you could do this with elabel (SSC).

                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input float(sex nationality child)
                1 3 6
                2 3 6
                2 4 6
                1 4 7
                1 8 7
                2 8 7
                end
                label define allvalue 1 "Male" 2 "Female" 3 "French" 4 "England" 8 "German" 6 "Yes" 7 "No"
                
                // Start here
                *ssc install elabel
                label values * allvalue
                
                    // #1 assumes variables have ~ 250 unique values or less [ limit for inlist() ]
                foreach var of varlist * {
                    levelsof `var' , local(levels) separate(,)
                    elabel copy allvalue `var':`var' iff inlist(#, `levels')
                }
                
                    // #2 should work for a large number unique values in a variable
                foreach var of varlist * {
                    levelsof `var' , local(levels)
                    label copy allvalue `var'
                    elabel define `var':`var' (`levels') (= @) , replace
                }
                Best
                Daniel

                Comment


                • #9
                  Thank you very much, Andrew and Daniel, your codes work perfectly with my dataset, and it solves my problem !

                  As I have mentioned, I have serious doubts that giving Vincent code that does what he wants would actually help him in the longer run.
                  I need to transform these value label like this because i use a function which create a dictionnary of variables (first in txt file and then in excel with vba macro).
                  With only one label value, every variable will be associated with "allvalue" and i don't want that in my dictionnary. With these changes, the dictionnary looks good.

                  Thank you again for your time, it help me alot!

                  Comment

                  Working...
                  X