Announcement

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

  • uselabel: how to also export values without a label

    Hi Statalist,

    I was trying to output a file which showcase all values (regardless of whether that value has a label) of a variable.

    Here are the codes I used:

    use data, clear
    uselabel, clear
    save label, replace

    however, this output only shows the values that have labels. There are some numeric variables in my data, for example: variable urban, it could have the following values
    values label
    1 urban
    2 rural
    99 (NO LABEL)


    I wonder how could I also output the value "99"? Any suggestions are much appreciated!

    Thank you in advance!


  • #2
    I do not use uselabel and you are asked to state the provenance of community-contributed commands.

    I was trying to output a file which showcase all values (regardless of whether that value has a label) of a variable.
    Output or export to where? Using decode and contract is enough to get these values in a dataset, which you can export in whichever format you like. See

    Code:
    help decode
    and

    Code:
    help contract
    Alternatively, if you always have short labels, a simple program such as the following can create a matrix with the information that you request.

    Code:
    cap prog drop labvals
    program labvals, rclass
        version 13.0
        syntax namelist
        tempname val lab labval
        qui preserve
        qui gen `val'= `namelist'
        qui decode(`namelist'), g(`lab')
        qui contract `val' `lab'
        qui replace `lab'= cond(!missing(`lab'), `=strtoname("`lab'")', "_")
        qui mkmat `val', mat(`labval') rownames(`lab')
        mat colname `labval'= "Values"
        return matrix labval = `labval'
        qui restore
    end
    
    *EXAMPLE
    sysuse auto, clear
    replace foreign= 99 in 1/7
    labvals foreign
    return list
    mat list r(labval)
    Res.:

    Code:
    . mat list r(labval)
    
    r(labval)[3,1]
              Values
    Domestic       0
     Foreign       1
           _      99

    Comment


    • #3
      Hi Andrew, the last code solution that you provided is exactly what I am looking for! I came up with the following solution which saves the output to a new .dta dataset

      sysuse auto, clear
      replace foreign= 99 in 1/7
      contract foreign
      decode foreign, gen(label)
      gen value=foreign

      This works perfectly! But then I have a question of how to save the output of "mat list r(labval)" into such a .dta dataset" -- Just curious! Thank you again Andrew! (the contract command is a perfect solution!)

      Comment


      • #4
        Hi again Andrew,

        I realized that the solution could only output the label values for one numeric variable. The reason why I used "uselabel" at the first place is that it output all numeric vars in a long format.

        For the "mat list r(labval)" solution you provided, I tried to add multiple varnames but it did not work. I wonder what direction I should go for adding more than one variable? Thank you!

        Comment


        • #5
          If you want to hold the result in a dataset, there is no need for matrices. It is unclear whether you need the values for only variables with labels or for all numeric variables in the dataset. I assume the latter, but getting the former only requires a minor modification.

          Code:
          *ASSIGN NAME TO TEMPORARY HOLDING FILE
          tempfile holding
          *OPEN DATASET HERE
          sysuse auto, clear
          *IDENTIFY NUMERIC VARS AND LOOP
          qui ds, has(type numeric)
          foreach var in `r(varlist)'{
              preserve
              cap decode `var', g(label)
              cap gen label= ""
              gen value= `var', before(label)
              contract value label
              gen varname= "`var'", before(value)
              drop _freq
              cap append using `holding'
              qui save `holding', replace
              restore
          }
          use `holding', clear
          Res.:

          Code:
          . l, sepby(varname)
          
               +---------------------------------+
               |      varname   value      label |
               |---------------------------------|
            1. |      foreign       0   Domestic |
            2. |      foreign       1    Foreign |
               |---------------------------------|
            3. |   gear_ratio    2.19            |
            4. |   gear_ratio    2.24            |
            5. |   gear_ratio    2.26            |
            6. |   gear_ratio    2.28            |
            7. |   gear_ratio    2.41            |
            8. |   gear_ratio    2.43            |
            9. |   gear_ratio    2.47            |
           10. |   gear_ratio    2.53            |
           11. |   gear_ratio    2.56            |
           12. |   gear_ratio    2.73            |
           13. |   gear_ratio    2.75            |
           14. |   gear_ratio    2.87            |
           15. |   gear_ratio    2.93            |
           16. |   gear_ratio    2.94            |
           17. |   gear_ratio    2.97            |
           18. |   gear_ratio    2.98            |
           19. |   gear_ratio    3.05            |
           20. |   gear_ratio    3.06            |
           21. |   gear_ratio    3.08            |
           22. |   gear_ratio    3.15            |
           23. |   gear_ratio     3.2            |
           24. |   gear_ratio    3.21            |
           25. |   gear_ratio    3.23            |
           26. |   gear_ratio     3.3            |
           27. |   gear_ratio    3.37            |
           28. |   gear_ratio    3.54            |
           29. |   gear_ratio    3.55            |
           30. |   gear_ratio    3.58            |
           31. |   gear_ratio    3.64            |
           32. |   gear_ratio     3.7            |
           33. |   gear_ratio    3.72            |
           34. |   gear_ratio    3.73            |
           35. |   gear_ratio    3.74            |
           36. |   gear_ratio    3.78            |
           37. |   gear_ratio    3.81            |
           38. |   gear_ratio    3.89            |
               |---------------------------------|
           39. | displacement      79            |
           40. | displacement      85            |
           41. | displacement      86            |
           42. | displacement      89            |
           43. | displacement      90            |
           44. | displacement      91            |
           45. | displacement      97            |
           46. | displacement      98            |
           47. | displacement     105            |
           48. | displacement     107            |
           49. | displacement     119            |
           50. | displacement     121            |
           51. | displacement     131            |
           52. | displacement     134            |
           53. | displacement     140            |
           54. | displacement     146            |
           55. | displacement     151            |
           56. | displacement     156            |
           57. | displacement     163            |
           58. | displacement     196            |
           59. | displacement     200            |
           60. | displacement     225            |
           61. | displacement     231            |
           62. | displacement     250            |
           63. | displacement     258            |
           64. | displacement     302            |
           65. | displacement     304            |
           66. | displacement     318            |
           67. | displacement     350            |
           68. | displacement     400            |
           69. | displacement     425            |
               |---------------------------------|
           70. |         turn      31            |
           71. |         turn      32            |
           72. |         turn      33            |
           73. |         turn      34            |
           74. |         turn      35            |
           75. |         turn      36            |
           76. |         turn      37            |
           77. |         turn      38            |
           78. |         turn      39            |
           79. |         turn      40            |
           80. |         turn      41            |
           81. |         turn      42            |
           82. |         turn      43            |
           83. |         turn      44            |
           84. |         turn      45            |
           85. |         turn      46            |
           86. |         turn      48            |
           87. |         turn      51            |
               |---------------------------------|
           88. |       length     142            |
           89. |       length     147            |
           90. |       length     149            |
           91. |       length     154            |
           92. |       length     155            |
           93. |       length     156            |
           94. |       length     157            |
           95. |       length     161            |
           96. |       length     163            |
           97. |       length     164            |
           98. |       length     165            |
           99. |       length     168            |
          100. |       length     169            |
          101. |       length     170            |
          102. |       length     172            |
          103. |       length     173            |
          104. |       length     174            |
          105. |       length     175            |
          106. |       length     177            |
          107. |       length     179            |
          108. |       length     180            |
          109. |       length     182            |
          110. |       length     184            |
          111. |       length     186            |
          112. |       length     189            |
          113. |       length     192            |
          114. |       length     193            |
          115. |       length     195            |
          116. |       length     196            |
          117. |       length     197            |
          118. |       length     198            |
          119. |       length     199            |
          120. |       length     200            |
          121. |       length     201            |
          122. |       length     203            |
          123. |       length     204            |
          124. |       length     206            |
          125. |       length     207            |
          126. |       length     212            |
          127. |       length     214            |
          128. |       length     217            |
          129. |       length     218            |
          130. |       length     220            |
          131. |       length     221            |
          132. |       length     222            |
          133. |       length     230            |
          134. |       length     233            |
               |---------------------------------|
          135. |       weight    1760            |
          136. |       weight    1800            |
          137. |       weight    1830            |
          138. |       weight    1930            |
          139. |       weight    1980            |
          140. |       weight    1990            |
          141. |       weight    2020            |
          142. |       weight    2040            |
          143. |       weight    2050            |
          144. |       weight    2070            |
          145. |       weight    2110            |
          146. |       weight    2120            |
          147. |       weight    2130            |
          148. |       weight    2160            |
          149. |       weight    2200            |
          150. |       weight    2230            |
          151. |       weight    2240            |
          152. |       weight    2280            |
          153. |       weight    2370            |
          154. |       weight    2410            |
          155. |       weight    2520            |
          156. |       weight    2580            |
          157. |       weight    2640            |
          158. |       weight    2650            |
          159. |       weight    2670            |
          160. |       weight    2690            |
          161. |       weight    2730            |
          162. |       weight    2750            |
          163. |       weight    2830            |
          164. |       weight    2930            |
          165. |       weight    3170            |
          166. |       weight    3180            |
          167. |       weight    3200            |
          168. |       weight    3210            |
          169. |       weight    3220            |
          170. |       weight    3250            |
          171. |       weight    3260            |
          172. |       weight    3280            |
          173. |       weight    3300            |
          174. |       weight    3310            |
          175. |       weight    3330            |
          176. |       weight    3350            |
          177. |       weight    3370            |
          178. |       weight    3400            |
          179. |       weight    3420            |
          180. |       weight    3430            |
          181. |       weight    3470            |
          182. |       weight    3600            |
          183. |       weight    3670            |
          184. |       weight    3690            |
          185. |       weight    3700            |
          186. |       weight    3720            |
          187. |       weight    3740            |
          188. |       weight    3830            |
          189. |       weight    3880            |
          190. |       weight    3900            |
          191. |       weight    4030            |
          192. |       weight    4060            |
          193. |       weight    4080            |
          194. |       weight    4130            |
          195. |       weight    4290            |
          196. |       weight    4330            |
          197. |       weight    4720            |
          198. |       weight    4840            |
               |---------------------------------|
          199. |        trunk       5            |
          200. |        trunk       6            |
          201. |        trunk       7            |
          202. |        trunk       8            |
          203. |        trunk       9            |
          204. |        trunk      10            |
          205. |        trunk      11            |
          206. |        trunk      12            |
          207. |        trunk      13            |
          208. |        trunk      14            |
          209. |        trunk      15            |
          210. |        trunk      16            |
          211. |        trunk      17            |
          212. |        trunk      18            |
          213. |        trunk      20            |
          214. |        trunk      21            |
          215. |        trunk      22            |
          216. |        trunk      23            |
               |---------------------------------|
          217. |     headroom     1.5            |
          218. |     headroom       2            |
          219. |     headroom     2.5            |
          220. |     headroom       3            |
          221. |     headroom     3.5            |
          222. |     headroom       4            |
          223. |     headroom     4.5            |
          224. |     headroom       5            |
               |---------------------------------|
          225. |        rep78       1            |
          226. |        rep78       2            |
          227. |        rep78       3            |
          228. |        rep78       4            |
          229. |        rep78       5            |
          230. |        rep78       .            |
               |---------------------------------|
          231. |          mpg      12            |
          232. |          mpg      14            |
          233. |          mpg      15            |
          234. |          mpg      16            |
          235. |          mpg      17            |
          236. |          mpg      18            |
          237. |          mpg      19            |
          238. |          mpg      20            |
          239. |          mpg      21            |
          240. |          mpg      22            |
          241. |          mpg      23            |
          242. |          mpg      24            |
          243. |          mpg      25            |
          244. |          mpg      26            |
          245. |          mpg      28            |
          246. |          mpg      29            |
          247. |          mpg      30            |
          248. |          mpg      31            |
          249. |          mpg      34            |
          250. |          mpg      35            |
          251. |          mpg      41            |
               |---------------------------------|
          252. |        price    3291            |
          253. |        price    3299            |
          254. |        price    3667            |
          255. |        price    3748            |
          256. |        price    3798            |
          257. |        price    3799            |
          258. |        price    3829            |
          259. |        price    3895            |
          260. |        price    3955            |
          261. |        price    3984            |
          262. |        price    3995            |
          263. |        price    4010            |
          264. |        price    4060            |
          265. |        price    4082            |
          266. |        price    4099            |
          267. |        price    4172            |
          268. |        price    4181            |
          269. |        price    4187            |
          270. |        price    4195            |
          271. |        price    4296            |
          272. |        price    4389            |
          273. |        price    4424            |
          274. |        price    4425            |
          275. |        price    4453            |
          276. |        price    4482            |
          277. |        price    4499            |
          278. |        price    4504            |
          279. |        price    4516            |
          280. |        price    4589            |
          281. |        price    4647            |
          282. |        price    4697            |
          283. |        price    4723            |
          284. |        price    4733            |
          285. |        price    4749            |
          286. |        price    4816            |
          287. |        price    4890            |
          288. |        price    4934            |
          289. |        price    5079            |
          290. |        price    5104            |
          291. |        price    5172            |
          292. |        price    5189            |
          293. |        price    5222            |
          294. |        price    5379            |
          295. |        price    5397            |
          296. |        price    5705            |
          297. |        price    5719            |
          298. |        price    5788            |
          299. |        price    5798            |
          300. |        price    5799            |
          301. |        price    5886            |
          302. |        price    5899            |
          303. |        price    6165            |
          304. |        price    6229            |
          305. |        price    6295            |
          306. |        price    6303            |
          307. |        price    6342            |
          308. |        price    6486            |
          309. |        price    6850            |
          310. |        price    7140            |
          311. |        price    7827            |
          312. |        price    8129            |
          313. |        price    8814            |
          314. |        price    9690            |
          315. |        price    9735            |
          316. |        price   10371            |
          317. |        price   10372            |
          318. |        price   11385            |
          319. |        price   11497            |
          320. |        price   11995            |
          321. |        price   12990            |
          322. |        price   13466            |
          323. |        price   13594            |
          324. |        price   14500            |
          325. |        price   15906            |
               +---------------------------------+
          
          .

          Comment


          • #6
            This works perfectly! Thank you Andrew! (I was assuming wanting all numeric values, even some are not attached to any labels)

            Comment

            Working...
            X