{"id":307,"date":"2024-12-31T17:15:20","date_gmt":"2024-12-31T07:15:20","guid":{"rendered":"https:\/\/xinranhu.com\/?p=307"},"modified":"2025-01-02T14:07:42","modified_gmt":"2025-01-02T04:07:42","slug":"random-stata-tricks-the-2024-supercut","status":"publish","type":"post","link":"https:\/\/xinranhu.com\/index.php\/2024\/12\/31\/random-stata-tricks-the-2024-supercut\/","title":{"rendered":"Random Stata tricks: The 2024 supercut"},"content":{"rendered":"\n<p>It&#8217;s the end of the year, and what&#8217;s more suitable to wrap it up than showing people all the thingamajigs I learned to do in Stata?<\/p>\n\n\n\n<p>What I&#8217;m going to show below might not be new to you. But if I was learning them for the first time in 2024, then there&#8217;s a fair chance that there exists at least one (1) individual on the internet who will find them useful!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>First and foremost, let&#8217;s start with something really funny: Stata&#8217;s do-file editor somehow allows you to <strong>insert your cursor at multiple locations simultaneously<\/strong>, just like <a href=\"https:\/\/bsky.app\/profile\/arthurturrell.bsky.social\/post\/3lelwzxyjoc2m\" data-type=\"link\" data-id=\"https:\/\/bsky.app\/profile\/arthurturrell.bsky.social\/post\/3lelwzxyjoc2m\" target=\"_blank\" rel=\"noreferrer noopener\">an actual programming IDE<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"238\" style=\"aspect-ratio: 496 \/ 238;\" width=\"496\" controls src=\"https:\/\/xinranhu.com\/wp-content\/uploads\/2024\/12\/Most-Useless-Stata-functionality.mp4\"><\/video><\/figure>\n\n\n\n<p>I have no idea why and how, but this turns out to be handy when you need to edit several lines of your code in a similar manner.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Next let&#8217;s talk about two things that I surprisingly had no idea about until this year. I&#8217;m grouping them as for me they serve the same purpose: <strong>Minimize unnecessary Stata outputs to the hard drive.<\/strong><\/p>\n\n\n\n<p>Seasoned Stata users might know where this is leading to: Temp files. <\/p>\n\n\n\n<p>Temp files are another kind of Stata macro that stands for placeholder file names. They are treated akin to locals &#8211; which means that if you save something in a temp file, Stata stores said stuff temporarily somewhere in its root folder, associates this temporarily stored stuff with the name assigned to the tempfile macro, and deletes the temporary file immediately when the code block is done executing. I find them handy for <strong>complicated data-cleaning work<\/strong> where I need to <strong>dice up the dataset in different ways and stitch them back together.<\/strong><\/p>\n\n\n\n<p>So two things: 1. This does not stop the file from ever being stored on your hard drive. But at least it no longer lies there forever! 2. Temp files are <strong>local<\/strong>s, so they are &#8220;gone&#8221; gone after running the code (Otherwise it defies the purpose isn&#8217;t it?).<\/p>\n\n\n\n<p>Alright, enough talking. Here is a sketch of how temp files can be used in said setting:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">Stata<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ Let's assume the working directory has been set and the dataset is just called &quot;data&quot;\nuse data, clear\ntempfile temp_data \n\n\/* Insert whatever data cleaning code here *\/\n\nsave `temp_data'\n\n\/\/ If later down the track you'll need to merge the saved temporary dataset with something else, it can be called just like any other local!\nmerge 1:1 id using `temp_data'\nsave The_actual_dataset, replace\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">\/\/ Let&#39;s assume the working directory has been set and the dataset is just called &quot;data&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">use<\/span><span style=\"color: #D4D4D4\"> data, <\/span><span style=\"color: #C586C0\">clear<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">tempfile<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">temp_data<\/span><span style=\"color: #D4D4D4\"> <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/* Insert whatever data cleaning code here *\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">save<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">temp_data<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ If later down the track you&#39;ll need to merge the saved temporary dataset with something else, it can be called just like any other local!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">merge<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> id using <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">temp_data<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">save<\/span><span style=\"color: #D4D4D4\"> The_actual_dataset, replace<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>The handy thing with temp files is that they can be used to store formats other than .dta files as well! Here is our second gimmick which solves one of my long-standing questions: How do you export a single .pdf file with all the graphs you wanted in Stata?<\/p>\n\n\n\n<p>It turns out that there is a set of commands called <strong>putpdf<\/strong> out there. Those commands quite literally put stuff into a (yet to be formally saved) .pdf file. Here I think things will make more sense if I start with a hypothetical example:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">Stata<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ Imagine that the dataset has already been loaded and we have a list of variables that each need a) A histogram; b) A kernel density plot.\nlocal var_list \/* Insert the variables here *\/\n\n******************************************\n* Plotting\n******************************************\nputpdf clear \/\/ Makes sure that there is no existing .pdf file in the background\nputpdf begin \/\/ Creates an empty .pdf file\n\nputpdf paragraph \/\/ Tells Stata &quot;We'll add content from here&quot;\n\ntempfile hist_temp\ntempfile kern_temp\n\nforeach var in varlist `var_list' {\n  hist `var' \/\/ Just vanilla code here as this is not the emphasis\n  graph export &quot;`hist_temp'&quot;, as(png) replace\n  putpdf image &quot;`hist_temp'&quot;\n  \n  kdensity `var' \/\/ Just vanilla code here as this is not the emphasis either\n  graph export &quot;`kern_temp'&quot;, as(png) replace\n  putpdf image &quot;`kern_temp'&quot;\n  \n  putpdf pagebreak \/\/ Adds a pagebreak\n\tputpdf paragraph\n\n}\n\nputpdf save &quot;Plots.pdf&quot;, replace \/\/ Saves the .pdf file to an actual location in the hard drive\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">\/\/ Imagine that the dataset has already been loaded and we have a list of variables that each need a) A histogram; b) A kernel density plot.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">local<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">var_list<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #6A9955\">\/* Insert the variables here *\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">******************************************<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">* Plotting<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">******************************************<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">putpdf <\/span><span style=\"color: #C586C0\">clear<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #6A9955\">\/\/ Makes sure that there is no existing .pdf file in the background<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">putpdf begin <\/span><span style=\"color: #6A9955\">\/\/ Creates an empty .pdf file<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">putpdf paragraph <\/span><span style=\"color: #6A9955\">\/\/ Tells Stata &quot;We&#39;ll add content from here&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">tempfile<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">hist_temp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">tempfile<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">kern_temp<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">foreach<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> varlist <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">var_list<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #C586C0\">hist<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #6A9955\">\/\/ Just vanilla code here as this is not the emphasis<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #C586C0\">graph<\/span><span style=\"color: #D4D4D4\"> export <\/span><span style=\"color: #CE9178\">&quot;`<\/span><span style=\"color: #4EC9B0\">hist_temp<\/span><span style=\"color: #CE9178\">&#39;&quot;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #DCDCAA\">as<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">png<\/span><span style=\"color: #D4D4D4\">) replace<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  putpdf image <\/span><span style=\"color: #CE9178\">&quot;`<\/span><span style=\"color: #4EC9B0\">hist_temp<\/span><span style=\"color: #CE9178\">&#39;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #C586C0\">kdensity<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #6A9955\">\/\/ Just vanilla code here as this is not the emphasis either<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #C586C0\">graph<\/span><span style=\"color: #D4D4D4\"> export <\/span><span style=\"color: #CE9178\">&quot;`<\/span><span style=\"color: #4EC9B0\">kern_temp<\/span><span style=\"color: #CE9178\">&#39;&quot;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #DCDCAA\">as<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">png<\/span><span style=\"color: #D4D4D4\">) replace<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  putpdf image <\/span><span style=\"color: #CE9178\">&quot;`<\/span><span style=\"color: #4EC9B0\">kern_temp<\/span><span style=\"color: #CE9178\">&#39;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  putpdf pagebreak <\/span><span style=\"color: #6A9955\">\/\/ Adds a pagebreak<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">\tputpdf paragraph<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">putpdf <\/span><span style=\"color: #569CD6\">save<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;Plots.pdf&quot;<\/span><span style=\"color: #D4D4D4\">, replace <\/span><span style=\"color: #6A9955\">\/\/ Saves the .pdf file to an actual location in the hard drive<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>The key thing I noticed when using the putpdf commands is that Stata would need the <strong>putpdf paragraph<\/strong> command whenever you start a new file\/page. But other than that, as long as you can keep track of your mental image of the yet-to-be-saved .pdf file, it&#8217;s pretty straightforward. <\/p>\n\n\n\n<p>The other thing is that you can see in this example that temp files can store things in whatever format as long as it is unambiguously specified.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Now onto some cool stuff. Say we have a local holding a list of variables to loop through, and now we need to only loop through some of them. There&#8217;s actually a cool way to do this that will make you feel like a real programmer (if you&#8217;re a goofball like me, that is)!<\/p>\n\n\n\n<p>We are talking about <strong>list manipulations<\/strong> here. Keep in mind that all list manipulations work with macros and are supposed to work with macros only. Hence any locals <strong>should not<\/strong> be wrapped with single quotation marks. <\/p>\n\n\n\n<p>Searching &#8220;<strong>macro lists<\/strong>&#8221; in Stata&#8217;s help file brings up the full list of things that can be done as list manipulations. I&#8217;ll offer two specific examples below to illustrate a bit of technical details:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">Stata<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/* Example 1 *\/\n\/\/ Imagine working with a long survey dataset where there are two observations (id=1 -husband\/2 - wife) per household (HHID). Here I'll reshape the hypothetical dataset to wide, and change the variable labels\n\nquiet ds \/\/ This the command that generates the r(varlist)\nlocal varlist `r(varlist)' \/\/ Exports a list of all variables\n\nlocal exclude HHID id \/\/ These are the two variables that do not need re-labeling after the reshape\nlocal allvars: list varlist-exclude \/\/ allvars becomes a local that includes all the variables excluding HHID and id\n\nforeach var of varlist `allvars' {\n  local lbl`var' : variable label `var' \/\/ Stores the original variable labels\n}\n\nreshape wide `allvars', i(HHID) j(id) string\n\n\/\/ This is a foreach-in loop because it's not actually looping through the variables in allvars\nforeach var in `allvars' {\n  label variable `var'_1 &quot;`lbl`var'' (Husband)&quot;\n  label variable `var'_2 &quot;`lbl`var'' (Wife)&quot;\n  \/\/ Loads the original variable label, appends the extra string, and labels the new variables\n}\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">\/* Example 1 *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ Imagine working with a long survey dataset where there are two observations (id=1 -husband\/2 - wife) per household (HHID). Here I&#39;ll reshape the hypothetical dataset to wide, and change the variable labels<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">quiet<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">ds<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #6A9955\">\/\/ This the command that generates the r(varlist)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">local<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">varlist<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">r<\/span><span style=\"color: #6A9955\">(varlist)<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #6A9955\">\/\/ Exports a list of all variables<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">local<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">exclude<\/span><span style=\"color: #D4D4D4\"> HHID id <\/span><span style=\"color: #6A9955\">\/\/ These are the two variables that do not need re-labeling after the reshape<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">local<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">allvars<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #C586C0\">list<\/span><span style=\"color: #D4D4D4\"> varlist-exclude <\/span><span style=\"color: #6A9955\">\/\/ allvars becomes a local that includes all the variables excluding HHID and id<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">foreach<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">of varlist<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">allvars<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #569CD6\">local<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">lbl<\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\"> : <\/span><span style=\"color: #569CD6\">variable label<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #6A9955\">\/\/ Stores the original variable labels<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">reshape<\/span><span style=\"color: #D4D4D4\"> wide <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">allvars<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #DCDCAA\">i<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">HHID<\/span><span style=\"color: #D4D4D4\">) <\/span><span style=\"color: #DCDCAA\">j<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">id<\/span><span style=\"color: #D4D4D4\">) string<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ This is a foreach-in loop because it&#39;s not actually looping through the variables in allvars<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">foreach<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">allvars<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #569CD6\">label<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">variable<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\">_1 <\/span><span style=\"color: #CE9178\">&quot;`<\/span><span style=\"color: #4EC9B0\">lbl<\/span><span style=\"color: #CE9178\">`<\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #CE9178\">&#39;&#39; (Husband)&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #569CD6\">label<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">variable<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\">_2 <\/span><span style=\"color: #CE9178\">&quot;`<\/span><span style=\"color: #4EC9B0\">lbl<\/span><span style=\"color: #CE9178\">`<\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #CE9178\">&#39;&#39; (Wife)&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\">\/\/ Loads the original variable label, appends the extra string, and labels the new variables<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">Stata<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/* Example 2 *\/\n\/\/ Imagine looping through a list of variables, where the sequence of the variables matter.\n\/\/ However, some of the variables are slightly different from the others (e.g., continuous vs. categorical) and requires a slightly different set of commands.\n\nlocal all_variables \/* Insert the variables here *\/\nlocal oddballs \/* Insert the &quot;different&quot; variables here *\/\n\nforeach var in `all_variables' {\n  if `: list var in oddballs'{\n    \/* The &quot;different&quot; set of commands *\/\n  }\n  else {\n    \/* The &quot;regular&quot; set of commands *\/\n  }\n}\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">\/* Example 2 *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ Imagine looping through a list of variables, where the sequence of the variables matter.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ However, some of the variables are slightly different from the others (e.g., continuous vs. categorical) and requires a slightly different set of commands.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">local<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">all_variables<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #6A9955\">\/* Insert the variables here *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">local<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">oddballs<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #6A9955\">\/* Insert the &quot;different&quot; variables here *\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">foreach<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">all_variables<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #569CD6\">list<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">var<\/span><span style=\"color: #D4D4D4\"> in <\/span><span style=\"color: #4EC9B0\">oddballs<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #C586C0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/* The &quot;different&quot; set of commands *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #C586C0\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/* The &quot;regular&quot; set of commands *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #C586C0\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>What you&#8217;ll notice from the two examples is that the result from a macro list manipulation can be either stored into another macro, or called directly. If called directly, Stata will treat the result as a local, and it needs to be wrapped by single quotation marks.<\/p>\n\n\n\n<p>List manipulations are supposed to work with <strong>local<\/strong>s, but if one of your macro lists is defined as a global they will still work as long as the global macro is appropriately referred to:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">Stata<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ Let's illustrate this with a modified version from example 1:\n\nquiet ds \nglobal varlist `r(varlist)' \n\nglobal exclude HHID id \nlocal allvars: list global(varlist)-global(exclude) \" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">\/\/ Let&#39;s illustrate this with a modified version from example 1:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">quiet<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">ds<\/span><span style=\"color: #D4D4D4\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">global<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">varlist<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">`<\/span><span style=\"color: #4EC9B0\">r<\/span><span style=\"color: #6A9955\">(varlist)<\/span><span style=\"color: #D4D4D4\">&#39;<\/span><span style=\"color: #D4D4D4\"> <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">global<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">exclude<\/span><span style=\"color: #D4D4D4\"> HHID id <\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">local<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">allvars<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #C586C0\">list<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">global<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">varlist<\/span><span style=\"color: #D4D4D4\">)-<\/span><span style=\"color: #DCDCAA\">global<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">exclude<\/span><span style=\"color: #D4D4D4\">) <\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>A global macro can be processed by local manipulations if you wrap its name with global().<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s the end of the year, and what&#8217;s more suitable to wrap it up than showing people all the thingamajigs I learned to do in Stata? What I&#8217;m going to show below might not be new to you. But if I was learning them for the first time in 2024, then there&#8217;s a fair chance [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_themeisle_gutenberg_block_has_review":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-307","post","type-post","status-publish","format-standard","hentry","category-economics"],"_links":{"self":[{"href":"https:\/\/xinranhu.com\/index.php\/wp-json\/wp\/v2\/posts\/307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinranhu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinranhu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinranhu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinranhu.com\/index.php\/wp-json\/wp\/v2\/comments?post=307"}],"version-history":[{"count":10,"href":"https:\/\/xinranhu.com\/index.php\/wp-json\/wp\/v2\/posts\/307\/revisions"}],"predecessor-version":[{"id":320,"href":"https:\/\/xinranhu.com\/index.php\/wp-json\/wp\/v2\/posts\/307\/revisions\/320"}],"wp:attachment":[{"href":"https:\/\/xinranhu.com\/index.php\/wp-json\/wp\/v2\/media?parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinranhu.com\/index.php\/wp-json\/wp\/v2\/categories?post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinranhu.com\/index.php\/wp-json\/wp\/v2\/tags?post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}