stata , Wrap local macro in double quotes that will be visible to esttab's cells() option


Wrap local macro in double quotes that will be visible to esttab's cells() option

Question:

Tag: stata

I am trying to lazily create a table of means and standard errors for a longish list of variables. It seems that the estout package from SSC and tabstat are the best tools, but I can't get the local macros to work properly to specify esttab's cells() option.

sysuse auto, clear

* build macro for `cells()` option
local i = 1
foreach v of varlist price weight displacement {
    local cells "`cells'" " `v'"
    if (`i' == 1) local cells "`cells'(fmt(%9.3gc))"
    local ++i
}

* properly built
display "`cells'"

* but does not work with `esttab`
estpost tabstat price weight displacement, statistics(mean semean)
esttab ., cells("`cells'")

This yields an "empty" table.

. esttab ., cells("`cells'")

-------------------------
                      (1)

                        b
-------------------------
-------------------------
N                      74
-------------------------

It seems that cells() needs to see double quotes, but my attempts to add them with single and double quotes at any point in the process. Is there a way to make this approach work? I would like to avoid manually generating the cells() argument.

* The following approach does work.
esttab ., cells("price(fmt(%9.3gc)) weight displacement")

This yields the correct table.

. esttab ., cells("price(fmt(%9.3gc)) weight displacement")

---------------------------------------------------
                      (1)                          

                    price       weight displacement
---------------------------------------------------
mean                6,165        3,019          197
semean                343         90.3         10.7
---------------------------------------------------
N                      74                          
---------------------------------------------------

Answer:

@Nick has already given a solution to the problem. He claims only stylistic changes were made, but I suspect more.

The double quotes originally used by the poster introduce an additional word in the definition of local cells. That is clear when we count the words contained in the local macro using the extended macro function : word count. @Richard works with three variables, but we count four words. Carefull inspection shows that the additional, surprise word, is "", introduced in the loop its first time around.

In this case, using display to check the contents of the local is misleading, because the command will simply do away with "". As a result, we see on screen "three" words. Displaying each word (one by one), more clearly shows that the first one is a blank.

What this means is you are coding something like:

esttab ., cells(""" price weight displacement")

when you really mean

esttab ., cells("price weight displacement")

Below I post some code consistent with this hypothesis. To simplify exposition, I have stripped away unnecessary complications from the original code.

sysuse auto, clear

// build macro
local i = 1
foreach v of varlist price weight displacement {
    local cells "`cells'" " `v'"
    *local cells `cells' `v'
}

// check contents of macro cells
local wc : word count "`cells'"   
display `wc'

forvalues i = 1/4 {
    local w`i' : word `i' of "`cells'" 
    display "`w`i''"
}

// display a test
local test "" " price" " weight" " displacement"
local wct : word count "`test'"
display `wct' // four words also

// more displays
display "`cells'" 
display """ price weight displacement" // same display result 

// tables

// post
quietly estpost tabstat price weight displacement, statistics(mean semean)

// original with error
esttab ., cells("`cells'")

// original with error after dereferencing the local macro cells
esttab ., cells(""" price weight displacement")

Nick's solution, that doesn't use double quotes, solves the problem.


Related:


Stata read date variable from MS SQL


sql-server,date,format,stata
I connected Stata via ODBC to a SQL Database. My problem is Stata reads date variables as Strings. In SQL they have a date format. How can I import a date variable in SQL as a date in Stata?...

Replace loop in Stata


replace,stata
I have two variables: patient id and date. Many patients on my database are duplicated. I want to keep the duplication, but apply to each patient the earliest appearing date. Ex: ID Date 1 8/9/07 1 6/3/07 1 11/15/08 2 8/6/06 2 8/6/06 2 11/5/09 would become ID Date 1...

Stata putexcel summary statistics by group to MS Excel


excel,stata
I'm trying to get the Stata command putexcel to give me summary statistics for a continuous variable, grouped by a categorical variable, one after another, in the same worksheet. This should be repeated/looped through a number of years, where each year has its own sheet. This poses two problems: using...

Stata: Saving to a specific folder


stata
I would like to save my outputs to a folder with the date of the analysis. My code looks like this local d = c(current_date) cd "c\RA-outputs" mkdir "`d'" twoway bar weeksum week graph export "c\RA-outputs\`d'\out1.png", as(png) replace However when I run this code I get an error saying the...

Stata- Stopping at the variable before a specified variable in a varlist


macros,stata
I'm stuck on a tricky data management question, which I need to do in Stata. I'm using Stata 13.1. I have 40+ datasets I need to work on using a subset of variables that is different in each dataset. I can't include the data or specific analysis I'm doing for...

Recode the same value pattern for all variables in Stata


binary,stata,recode
In my dataset, I have a bunch of Yes/No type variables. For some reason, "Yes" is coded as 1 and "No" is coded as 2 instead of 0. Now I want to recode 2 to 0 based on the value label "No". How can I do it without having to...

Linear algebra on dataframes in Stata vs R/Python [closed]


python,r,stata
In R (and I think Panda in Python), datasets roughly correspond to a list of vectors. Before applying linear algebra on a set of numeric variables in a dataset, one first need to convert them into a matrix (see for instance the code in R lm). This requires a deep...

Find social network components in Stata


grouping,social-networking,stata
[I copied part of the below example from a separate post and changed it to suit my specific needs] pos_1 pos_2 2 4 2 5 1 2 3 9 4 2 9 3 The above is read as person_2 is connected to person_4,...,person_4 is connected to person_2, and person_9 is...

Create a variable by dividing a variable by IQR in Stata


stata
How could I create a variable by dividing it by an IQR? I have done it through a long way as follows. Sample data and code is the following: use http://www.ats.ucla.edu/stat/stata/notes/hsb2, clear foreach var of varlist read-socst { egen `var'75 = pctile(`var'), p(75) egen `var'25 = pctile(`var'), p(25) gen `var'q...

Stata: Keep only observations with minimum, maximum and median value of a given variable


select,stata,vlookup
In Stata, I have a dataset with two variables: id and var, and say 1000 observations. The variable var is of type float and takes distinct values for all observations. I would like to keep only the three observations where var is either the minimum of var, the maximum of...

Post e(b) vector from a custom program in Stata


stata
I wrote a program that computes a weighted regression and now I want my estimation results to be stored as an e(b) vector so that the bootstrap command can easily access the results, but I keep getting an error. My program looks like: capture program drop mytest program mytest, eclass...

R equivalent of Stata *


r,stata
In Stata, if I have these variables: var1, var2, var3, var4, var5, and var6, I can select all of them with the command var*. Does R have a similar functionality?

Export average value of one variable over 2 other variables


stata
I can use tabout var1 var2...style(csv) to export a matrix-like object with var1 on one axis, var2 on the other, and the frequencies that each combination of the values var1 and var2 occur. Is it possible to use tabout or some other commmand (preferably native to Stata) to do something...

Pairwise correlations over rolling periods ignoring double calculations


matrix,stata,correlation
I am trying to compute pairwise correlations over rolling windows for n= 40 variables where all rolled pairwise correlations for 2 given variables are saved in a new variable. My dataset has the following structure: Date V1 V2 V3 . . . 01/01/2009 0.3 0.6 0.5 02/01/2009 0.1 0.5 0.2...

Stata: Storing only part of a FE regression output for graphing


stata
I am running a regression with two fixed effects categories (country and year, is economic macro data). Since I am using xtreg, one is autohid, but the other is a variable: xtreg fiveyearyg taxratio i.year if taxratiocut == 1, i(wbcode1) fe cluster(wbcode1) estimates store yi I am running a number...

Stata overwrite all observations in cross section except last 20 non NA


panel,stata
I have a large strongly unbalanced panel in Stata, where each cross section only has a few observations, and the rest is NA (.). I want to overwrite all non NA observations that are not the last 20 non NA observations, in each cross section. I'm not sure how to...

Stata: How to name a variable with a value


stata
I would like to create a variable that takes a name of a value in particular cell. For example my data set looks like this var1 count xx 1 xc 2 xv 3 xj 4 I would like to create 4 new variables that take names from the values of...

read Excel cells into Stata global as variables


excel,variables,import,stata
There are several panel datasets I'd like to join. The observations in these datasets are identified by an id variable and a variable identifying the time the observation was made. All datasets include some variables I need, some I don't need and never the same variables (excluding the id and...

Stata - Cohort Study - Display Crude Risk Ratio (like r(rr_crude))


stata
I'm starting to use Stata 14. I'm trying to do some basic risk ratio analysis, but I don't know how to extract single results. Given the following code: clear all webuse ugdp cs case exposed [fw=pop], by(age) we get an output with four risk ratios, for both age categories, a...

Reuse lines of code in Stata, Similar to JavaScript function?


stata
I have some lines of code in a Stata do-file that I would like to reuse/execute and different points in the do file. Similar to a JavaScript function... however I do not necessarily need an input variable. For example, I have some code: *code to reuse foreach x in test1...

Translating Stata to R: collapse


r,data.table,stata,code-translation
Just came across a .do file that I need to translate into R because I don't have a Stata license; my Stata is rusty, so can someone confirm that the code is doing what I think it is? Here's the Stata code: collapse (min) MinPctCollected = PctCollected /// (mean) AvgPctCollected...

Stata: order a dataset using a custom sorting order


sorting,stata
I have a dataset where numeric variable VARSORT takes only 3 values: 10, 20 and 30 (there are no missings). I would like to sort observations based on VARSORT but where the custom sort order would be the following : 20 first, then 10, then 30. Is it possible to...

How do I save the relative row or column frequencies after tab twoway in a matrix?


stata
If I run this code: sysuse auto, clear tab rep78 foreign, nofreq row matcell(freqs) matrix list freqs it's clear that tab only saved the actual counts in each cell, not the frequencies that were calculated and displayed with the nofreq row options. How do I save these relative frequencies in...

How to find maximum distance apart of values within a variable


stata
I create a working example dataset: input /// group value 1 3 1 2 1 3 2 4 2 6 2 7 3 4 3 4 3 4 3 4 4 17 4 2 5 3 5 5 5 12 end My goal is to figure out the maximum distance...

Marginal effect of interaction variable in probit regression using Stata


stata
I am running a probit regression with an interaction between one continuous and one dummy variable. The coefficient is displayed in the regression output but when I look at the marginal effects the interaction is missing. How can I get the marginal effect of the interaction variable? probit move_right c.real_income_change_percent##i.gender...

Run a regression of countries by quartiles for a specific year


stata
I am exploring an effect that I think will vary by GDP levels, from a data set that has, vertically, country and year (1960 to 2015), so each country label is on 55 rows. I ran sort year by year: egen yrank = xtile(rgdp), nquantiles(4) which tags every year row...

How to add Stata programs in subdirectories?


stata
I'd like to add a directory containing .ado files to the ado-path. This directory contains several subdirectories, corresponding to different projects. The .ado files are in these subdirectories. However, when I type adopath + directory, commands in the .ado files are not recognized by Stata. I need to enter adopath...

How to point Stata at the same help file for two different commands?


stata
Stata's swilk command performs the Shapiro-Wilk test for non-normality. Likewise, Stata's sfrancia performs the Shapiro-Francia test for non-normality. However, both commands share the same help file swilk.sthlp. How does Stata know that a single help file serves for more than one command? For example, how does Stata know that swilk.sthlp...

Align bars in ciplot


plot,stata
I'm working with the ciplot graphing module for Stata and am encountering a problem with the alignment of bars when I use the by() option. Here's a trivial example demonstrating the issue: webuse citytemp, clear ciplot heatdd cooldd, by(region) horizontal recast(conn) So, the graph shows means and confidence intervals for...

Is there a shortkey or command for the “Clear results” function


stata,shortcut
There's no shortkey right to the Clear results menu item in the Edit menu. I would like to be able to clear the main screen without using the mouse. I'm using Stata 13 on Windows 2008 Server.

Stata - panel data from column data


panel,stata,reshape
Panel data newbie here! I have data in Stata in the following format: Name Company1 Company2 Company3 Company4 Company5 Company6 1985 6.0781 2.4766 1.4258 2.6508 13.2083 1986 6.4844 3.0938 2.1953 3.1351 15.7917 1987 10.1563 .2769 5.7109 3.6406 4.4058 15.5833 1988 10.4688 .4219 5.125 3.75 3.6767 8.1667 1989 11.0625 .4289 5.4453...

how to: Separate a continuous variable by % proportions?


statistics,stata
I have a continuous variable (in this case, fees spent). How do I determine % spending cutoffs? i.e. how do I know what dollar amount separates the bottom 50% from the top 50% (similarly for any other % I may be interested in). Thank you very much for any help

Defining groups within an interval


group,stata
In Stata with the following data ID Date 1 1/1/2010 2 1/1/2010 3 1/4/2010 4 1/5/2010 5 1/8/2010 6 1/10/2010 7 1/11/2010 I am trying to create a variable Dummyi that gives a unique variable to all of the IDs that occurred within three days (before or after) of the...

Stata: refer to categorical fields by their labels instead of their numbers


stata
I am trying to use categorical variables more efficiently. Suppose I have a categorical variable phone, which has the following values: ---------------------- phone | Freq. ----------+----------- Landline | 223 Mobile | 49,297 Both | 1,308 I want to run a command something like this: sum x if phone == Mobile...

Dropping observations in Stata based on length?


stata
I have a string variable in Stata called Cod. I want to drop the observations such that Cod has less than 16 characters. Any suggestion?

Replace values within group


stata
I have a following data where I am trying to replace income for years 1980 and 1981 with that of year 1979 (no change for year 1978) [for each state]. state year size income 1 1978 1 1000 1 1978 1.5 100 1 1978 2 5000 1 1979 1 3779.736...

How to export Stata xtcsd test results?


stata,panel-data
I would like to export results of cross section dependence tests for 12 panel data sets to a table in order to compare them with similar tests done with different software. Below is the regression and test instruction example from the xtcsd help page (unfortunately the example dataset is not...

How can I change the value stored in local macro from string to numeric in Stata?


types,macros,global,local,stata
I want to generate a variable with lagged year depending on the year stored in "$S_DATE" macro I have stored the year in date macro: . local date substr("$S_DATE",8,.) . display `date' 2015 And I want to generate the new variable with gen start_year =`date'- y_passed where y_passed is a...

Mutually exclusive conditionals don't work in Stata


if-statement,stata
I have some data on Stata with some variables like logTA and class. I have more than a thousand observations and logTA doesn't have any missing values. Data looks like this: logTA class -------- -------- . . 21.26871 . Now, what I want to do is to assign values to...

Drop random effects parameters from output table in Stata


stata,mixed-models
I want to create a regression table (using esttab) from a mixed-effects regression estimated via xtmixed in Stata, but I want the output without the random effects parameters. How can I drop the random effects parameters from the output table? E.g., in the case of two variables... xtmixed (Dependent Variable)...

Using “input” command in forvalues; Stata acts like I hit break


loops,input,stata
I am trying to create empty observations for a number of fruits and a number of years. I thought it would be very straightforward, but Stata acts like I hit the "Break" key after the first time it enters the loop. What am I doing wrong? clear all gen fruit...

Stata: Retrieve variable label in a macro


variables,stata,labels
I'm generating graphs for several variables using a do-file, I would like to be able to retrieve a variable label (so that I could use it for the graph title). In my dreams, something along those lines: sysuse auto, replace local pricelabel = varlab(price) display "Label for price variable is...

Dynamic forecasting (arima) with multiple regressors in Stata


dynamic,stata,forecasting
I have a small time series dataset, a sample of which is below: year AvgU5MR AvgPov AvgEnrol 2000 126.9307 41.0109 67.11833 2001 123.4138 39.9748 68.66798 2002 119.93 45.85194 65.82739 2003 116.4923 55.3706 69.17756 2004 113.1362 32.63662 70.83884 2005 109.9008 41.08603 75.35649 2006 106.816 43.45722 75.98755 2007 103.8878 19.19114 76.86299 2008...

most efficient I/O setup between Stata and Python (Pandas)


python,csv,pandas,io,stata
I am using Stata to process some data, export the data in a csv file and load it in Python using the pandas read_csv function. The problem is that everything is so slow. Exporting from Stata to a csv file takes ages (exporting in the dta Stata format is much...

How to reshape long to wide data in Stata?


stata,data-management
I have the following data: id tests testvalue 1 A 4 1 B 5 1 C 3 1 D 3 2 A 3 2 B 3 3 C 3 3 D 4 4 A 3 4 B 5 4 A 1 4 B 3 I would like to change the...

When was a file used by another program


osx,performance,terminal,stata
I have a file with a series of 750 csv files. I wrote a Stata that runs through each of these files and performs a single task. The files are quite big, and my program has been running for more than 12 hours. Is there is a way to know...

Create flexible ggplot2 theme that 1) makes the legend and titles larger, 2) will look good irrespective of the final dimensions


r,ggplot2,stata
I currently am one of the few R users in my company, which consists predominantly of stata users. One problem I've had with making plots using ggplot2 is that the default (theme_grey()) settings have much smaller axis font and a smaller legend than what is found in stata. Moreover, in...

Stata: looping and appending


stata
I have a situation where I need to import all the files in a directory and append them. My code is this local files : dir "C:\Users\xx" files "*.xls" local n: word count `files' tokenize ``files'' cd "C:\Users\xx" forval k =1/`n'{ foreach file in `files' { import excel "`file'", sheet("Time...

150x150 crosstab in stata, showing timeseries movement between categories


time-series,stata,crosstab
I'm a bit over my head here and I hope you can help me, or at least point me in the right direction. I got a massive dataset (5.8 mio observations per year, over 14 years), which deals with individuals' occupation over time. I need to sum up the changes...

Retain the cluster number for each member of a cluster within an id variable


stata
I would like to label how many unique clusters of data are in a longitudinal dataset and have each member of the cluster carry the cluster count. Distinct clusters are those sharing a set of dates within an id. The order of those distinct cluster relative to previous (earlier) clusters...