{"id":878,"date":"2021-08-04T11:00:09","date_gmt":"2021-08-04T15:00:09","guid":{"rendered":"https:\/\/blog.uvm.edu\/tbplante\/?p=878"},"modified":"2021-08-04T14:03:41","modified_gmt":"2021-08-04T18:03:41","slug":"extracting-variable-labels-and-categorical-ordinal-value-labels-in-stata","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2021\/08\/04\/extracting-variable-labels-and-categorical-ordinal-value-labels-in-stata\/","title":{"rendered":"Extracting variable labels and categorical\/ordinal value labels in Stata"},"content":{"rendered":"\n<p>Stata allows the labeling of variables and also the individual values of categorical or ordinal variable values. For example, in the &#8211;sysuse auto&#8211; database, &#8220;foreign&#8221; is labeled as &#8220;Car origin&#8221;, 0 is &#8220;Domestic&#8221;, and 1 is &#8220;Foreign&#8221;. It isn&#8217;t terribly intuitive to extract the variable label of foreign (here, &#8220;Car origin&#8221;) or the labels from the categorical values (here, &#8220;Domestic&#8221; and &#8220;Foreign&#8221;). <\/p>\n\n\n\n<p>Here&#8217;s a script that you might find helpful to extract these labels and save them as macros that can later be called back. This example generates a second string variable that applies those labels. I use this sort of code to automate the labeling of figures with the value labels, but this is a pretty simple example for now. <\/p>\n\n\n\n<p>Remember to run the entire script from top to bottom or else Stata might drop your macros. Good luck!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sysuse auto, clear\n\/\/ Note: saving variable label details at \n\/\/       --help macro--, under \"Macro functions \n\/\/       for extracting data attributes\"\n\/\/ \n\/\/ 1. Extract the label for the variable itself.\n\/\/    If we look at the --codebook-- for the \n\/\/    variable \"foreign\", we see...\n\/\/\ncodebook foreign\n\/\/\n\/\/    ...that the label for \"foreign\" is \"Car \n\/\/    origin\" (see it in the top right of \n\/\/    the output).  Here's how we grab the \n\/\/    label of \"foreign\", save it as a macro, \n\/\/    and print it.\n\/\/\nlocal foreign_lab: variable label foreign\ndi \"`foreign_lab'\"\n\/\/\n\/\/ 2. Extract the label for the values of the variable.\n\/\/    If we look at --codebook-- again for \"foreign\", \n\/\/    we see...\n\/\/\ncodebook foreign\n\/\/ \n\/\/    ...that 0 is \"Domestic\" and 1 is \"Foreign\". \n\/\/    Here's how to grab those labels, save macros, \n\/\/    and print them\n\/\/\nlocal foreign_vallab_0: label (foreign) 0 \nlocal foreign_vallab_1: label (foreign) 1 \ndi \"The label of `foreign_lab' 0 is `foreign_vallab_0' and 1 is `foreign_vallab_1'\"\n\/\/\n\/\/ 3. Now you can make a variable for the value labels.\n\/\/  \ngen strL foreign_vallab = \"\" \/\/ this makes a string \nreplace foreign_vallab=\"`foreign_vallab_0'\" if foreign==0\nreplace foreign_vallab=\"`foreign_vallab_1'\" if foreign==1\n\/\/ \n\/\/ 4. You can also label this new string variable \n\/\/    using the label from #1\n\/\/ \nlabel variable foreign_vallab \"`foreign_lab' as string\"\n\/\/ \n\/\/ BONUS: You can automate this with a loop, using \n\/\/    --levelsof-- to extract options for each \n\/\/    categorical variable. There is only one \n\/\/    labeled categorical variable in this dataset \n\/\/    (foreign) so this loop only uses the single one. \n\/\/ \nsysuse auto, clear\nforeach x in foreign {\n\tlocal `x'_lab: variable label `x' \/\/ #1 from above\n\tgen strL `x'_vallab = \"\" \/\/ start of #3\n\tlabel variable `x'_vallab \"``x'_lab' string\"\n\tlevelsof `x', local(valrange)\n\tforeach n of numlist `valrange' { \n\t\tlocal `x'_vallab_`n': label (`x') `n' \/\/ #2 from above\n\t\treplace `x'_vallab = \"``x'_vallab_`n''\" if `x' == `n' \/\/ end of #3\n\t}\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Stata allows the labeling of variables and also the individual values of categorical or ordinal variable values. For example, in the &#8211;sysuse auto&#8211; database, &#8220;foreign&#8221; is labeled as &#8220;Car origin&#8221;, 0 is &#8220;Domestic&#8221;, and 1 is &#8220;Foreign&#8221;. It isn&#8217;t terribly intuitive to extract the variable label of foreign (here, &#8220;Car origin&#8221;) or the labels from &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2021\/08\/04\/extracting-variable-labels-and-categorical-ordinal-value-labels-in-stata\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Extracting variable labels and categorical\/ordinal value labels in Stata<\/span><\/a><\/p>\n","protected":false},"author":4473,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[477491],"tags":[],"class_list":["post-878","post","type-post","status-publish","format-standard","hentry","category-stata-code"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/users\/4473"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/comments?post=878"}],"version-history":[{"count":3,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/878\/revisions"}],"predecessor-version":[{"id":882,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/878\/revisions\/882"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}