# Questions tagged [stata]

1180 questions

1

votes

1

answer

387

Views

### Equivalent of Stata tab command in R

I'm trying to find out what the Stata command tab x y if z>1 would be in R.
Other than d %>% filter (z>1).

1

votes

2

answer

143

Views

### Tabulate a combined matrix of coefficients / t-statistics and export in LaTeX

I have many regressions (one per year, over fifteen years) for four outcome variables.
I have created two 15x4 matrices: one with the coefficients and one with the t-statistics. I am stuck now that I want to tabulate them in the classic way, each row having the coefficients for each model and under...

1

votes

2

answer

220

Views

### Robust standard errors for negative binomial regression in R do not match those from Stata

I am replicating a negative binomial regression model in R. When calculating robust standard errors, the output does not match Stata output of standard errors.
The original Stata code is
nbreg displaced eei lcostofwar cfughh roadskm lpopdensity ltkilled, robust nolog
I have attempted both manual...

1

votes

2

answer

44

Views

### xlabel in tableplot does not behave as expected

I have produced a graph using the community-contributed command tableplot.
The graph itself is fine, but on the x-axis there are a lot of years, which overlap and are not legible. I have thus tried to label only every decade or something similar. My initial attempts did not work, so I checked the S...

1

votes

2

answer

67

Views

### Convert strings to HRF monthly dates

I imported from Excel as variable names, and reshaped into 'observations', a series of strings (5s) that represent dates.
These dates (stored in the variable daate) have the form ddnn20Y: 1108, which means 1 Jan 2008; 1912, which means 1 Sep 2012; or 11210, which means 1 Dec 2010. That is they are...

1

votes

0

answer

273

Views

### Stata: Creating lags for data with multiple imputations

I am working with Stata and I have panel data set by countrycode and year, with multiple imputations.
When I try to sort data and command:
sort countrycode year
, then for some reason the imputed dataset loses variability, i.e. all the observations for non-imputed variable are the same.
E.g. for US...

1

votes

0

answer

16

Views

### Defining error correlation structure in Stata

I am trying to run a Probit model in Stata. I have to define the following error correlation matrix:
**Row 1:** 1, 0, a;
**Row 2:** 0, 1, a;
**Row 3:** a, a, 1;
Other than my parameter estimations, I have to estimate the unknown “a” in the above matrix. It would be great if a...

1

votes

0

answer

235

Views

### Integration Stata 14 and Sublime Text 3 doesn't run the .do file

I installed StataEditor and Pywin32 as suggested at: https://github.com/mattiasnordin/StataEditor. However, when I click Ctrl + D in Sublime Text Stata is opened but doesn't run the selected lines of code.
Can anybody help me?
Thank you!

1

votes

1

answer

49

Views

### Using two different versions of Stata

I am working in two locations, in one I am using Stata 13 and in the other Stata 14.
Can I build a do-file that works in both versions even if some specific command has changed?
For instance, the following code will not work using Stata 13
sysuse auto, clear
ci means mpg price, level(90)
but this on...

1

votes

0

answer

41

Views

### Using iweights in R

I am using R to do multiple imputation and would like to do a regression on the imputed results in R.
The code from Stata is the following:
mi estimate, dots: regress Direct_Violence gender threat political edu1 edu2 edu3 ///
inc1 inc2 year03 year04 rel1 rel2 rel3 age [iweight=weight]
This is just...

1

votes

0

answer

42

Views

### Why do very short bars in histogram appear “negative”?

I have found a weird behavior of Stata 15 when plotting a histogram that has some very short bars. The bars representing a (relatively) very low number of observations appear 'negative', that is they are plotted to reach below zero.
Below is a minimal reproducible example of the behavior, with the f...

1

votes

2

answer

68

Views

### Saving significance of a regression

I am running several simple regressions and I wish to save the value of the significance (P > |t|) of a regression for a given coefficient in a local macro.
For example, I know that:
local consCoeff = _b[_cons]
will save the coefficient for the constant, and that with _se[_cons] I can get the standa...

1

votes

0

answer

105

Views

### Differences between R and Stata in handling unidentified categorical variables

I am attempting to use the mlogit package in R to model a student's college major choice at graduation, conditional on in-major GPA, log family income, and first chosen major. First chosen major is a factor variable with all of the possible choices in majorcode except for 6, which represents droppin...

1

votes

0

answer

39

Views

### R and Stata performing differently with large datasets [duplicate]

This question already has an answer here:
Forcing R (and Rstudio) to use the virtual memory on Windows
1 answer
R: How to quickly read large .dta files without RAM Limitations
2 answers
I have a dataset with 40,000,000 observations and 23 variables. It is written is Stata format (.dta) and 4.4 Gb...

1

votes

0

answer

66

Views

### Mapping x and y values to screen coordinates

How can I map user-specified x and y values (in whatever scale) to
screen coordinates? This is what Stata internally does when it produces a graph.
For example:
clear
set seed 1234567890
set obs 500
generate y = rnormal(434.80132, 237.89369)
generate x = rnormal(0.08, 0.04)
twoway scatter y x
class...

1

votes

0

answer

31

Views

### Transposing axes in existing graph

Consider the following toy graph:
clear
set seed 1234567890
set obs 500
generate x = rnormal(100, 150)
generate y = rnormal(200, 300)
twoway scatter y x
How can I swap the axes after the graph is created?
For example:
Alternatively, is there a way to programmatically rotate the graph by 90 degrees?

1

votes

2

answer

11.7k

Views

### F-test on Fixed Effects in R (Panel Data)

I am trying to do an F-test on the joint significance of fixed effects (individual-specific dummy variables) on a panel data OLS regression (in R), however I haven't found a way to accomplish this for a large number of fixed effects. Ideally, I would use a function in the plm package, however I hav...

1

votes

1

answer

2.5k

Views

### Add a column of differences to tables of summary statistics in Stata

If I make a two way summary statistics table in Stata using table, can I add another column that is the difference of two other columns?
Say that I have three variables (a, b, c). I generate quintiles on a and b then generate a two-way table of means of c in each quintile-quintile intersection. I wo...

1

votes

2

answer

743

Views

### Mlogit macro with dummy variables

I am new to Stata and macros.
I am trying to loop over several variables to generate estimates from the mlogit command and then save them in datasets. That portion is working well.
The problem I have is a categorical variable that I need to split into dummy variables:
global mypath '/Volumes/NO N...

1

votes

1

answer

95

Views

### Stata macro and for loop when there are quotes and numbers

Suppose you have the macro
global LabNames '3M' 'ABBOTT' 'MERCK SHARP DOHME'
I am using the quotes so that the words are correctly grouped (MERCK SHARP DOHME is one company, not three different ones). I am trying to write a program that goes over a variable and replaces it when it has one of the...

1

votes

1

answer

396

Views

### Storing the cluster robust standard error to create a new variable— Stata 12 for Mac

I need to store the value for the cluster robust standard error in order to use it to create a new variable.
I am able to get the cluster robust standard error with the mean command, but stata does not store this value.
Do you have any suggestions about how to calculate the cluster robust standard e...

1

votes

1

answer

3k

Views

### Stata--Dates Conversion

I have dates in a CSV, and this is the first time I am running into this problem that when converting the dates from the CSV to Stata I am getting missing values. I usually use:
I have a variable from Excel, 'xdate'
gen stata_date = date(xdate, 'mdy')
This usually works, but for this data set I am g...

1

votes

4

answer

908

Views

### Turning project data into a relationship matrix

My data set a list of people either working together or alone.
I have have a row for each project and columns with names of all the people who worked on that project. If column 2 is the first empty column given a row it was a solo job, if column 4 is the first empty column given a row then there wer...

1

votes

1

answer

99

Views

### Group Unique ID

In stata if I have a list if groups:
XYZ
ABC
ABC
BCH
JSA
BCH
XYZ
How I get each group to have a unique ID in a second column after sorting, for example:
ABC 1
BCH 2
JSA 3
XYZ 4

1

votes

2

answer

2.4k

Views

### Calling Python from Stata

This is probably very easy, but after looking through documentation and possible examples online for the past several hours I cannot figure it out.
I have a large dataset (a spreadsheet) that gets heavily cleaned by a DO file. In the DO file I then want to save certain variables of the cleaned data...

1

votes

1

answer

1.5k

Views

### What could be causing errors when estimating coefficients with xtgls in stata for unbalanced panel data over 4 years?

I am using unbalanced panel data for 4 years. In trying to decide which time variant model (xtgls, xtreg, re, or xtgee) is most appropriate for my analysis, I am trying to estimate coefficients for xtgls under both the homoskedasticity and hetero assumptions. When I run this model with the hetero op...

1

votes

1

answer

447

Views

### Stata moving products

Using Stata I want a formula (line of code) that takes all of the previous entries for a given group G at a given cell and returns the product for all of the values at that cell and above. For example:
G X Y
1 1 1
1 2 2
1 6 12
1 3 36
2 2 2
2 4 8
3 2 2...

1

votes

1

answer

578

Views

### Stata Panel: How to assign value to a group, merge?

I have a panel of firms (id, year). Each firm also belongs to a specific country. Now I would like to add a GDP time series by country.
The goal is that e.g. each firm from the US is assigned the US' GDP of that year, etc.
Can anyone tell me how to go about that? Is there some way to use the merge...

1

votes

1

answer

130

Views

### Coding ranges in Stata

I was given a dataset that is VERY crude. One dataset gives me a code for each X variable (a 4 digit code) and then a text file that explains what each code means. Weirdly a code can mean something or a range could mean the same thing. For example:
X
2321
2322
2341
2520
2572
4000
4001
4002
4100
The...

1

votes

1

answer

141

Views

### Checking If against multiple values

I'm using ssc install vallist to generate a list of applicable countries that outputs a macro something like:
r(list) : '32 36 348 392 410 76 112 152 156 203'
Optionally, this can be returned separated by commas instead of spaces if it's helpful)
I then want to tab a variable if it has a country tha...

1

votes

1

answer

438

Views

### How to replace a zero-valued answer by its respective average value?

I have a household data set which includes expenditures for various foods. I categorized them into main food groups and price is obtained by dividing the expenditure value by quantity. For some households price comes as zero since their consumption with respect to the corresponding food group is zer...

1

votes

2

answer

8.8k

Views

### Stata ambiguous abbreviation r(111)

I am trying to draw marginplot using stata12. I am running the following code:
margins, at(FuncVariant =(0(0.2) 1)) over(Platform)
Following is the error:
FuncVariant ambiguous abbreviation r(111);
I have the following variables like
FuncVariant :
FuncVariant
FuncVariant_mean
FuncVariant_W
Is that...

1

votes

1

answer

7.6k

Views

### Export variable label, value label, and variable notes to a dataset (or a table)

Consider the following example dataset:
member like deposit age
1 1 9997 22
2 2 892 23
1 1 267 34
1 9 1728 54
3 1 9999 22
1 2 2123 34
9 2 445 13
1 1 928 22
1 1 276 34
2 1 . 23
2...

1

votes

1

answer

90

Views

### Stata: Reading in data and specifying var type

I am importing a dataset into Stata with a lot of variables (~250), a majority of which need to be read into Stata as strings, so I was wandering if instead of writing a painfully long dictionary file, I could just tell Stata to read all variables as strings, and then go back in and convert the few...

1

votes

1

answer

206

Views

### Data precision in Stata

This is the trivial problem and I just wanted to know what is happening here.
Following is my sample data for which I am trying to find the row max in stata
x1 x2 x2
70001102 70001102 70001102
70001102 70001102 70001102
Now, I used the following code to find the...

1

votes

1

answer

71

Views

### Prevent gVim from returning control to command line (when called from Stata)

When I call gVim from Stata with shell (or equivalently with !) Stata doesn't wait for the command to finish and continues on with the .do file. I usually specify a short sleep and everything works great (discussed on the Stata mailing list here).
But sometimes the gVim call is lengthy and the lengt...

1

votes

1

answer

5.1k

Views

### How to constrain parameters in regression models in Stata?

I am looking for a way to impose constraints on some of the parameters in a regression model in Stata. More specifically, it is a FE panel data model estimated with xtreg. I have searched the documentation and the web but with no luck so far.
Is there a way to constrain one or two parameters in the...

1

votes

2

answer

468

Views

### How to get running maximum in Stata?

I would like to get the running maximum by writing Stata code.
I think I am quite close:
gen ctrhigh`iv' = max(ctr, L1.ctr, L2.ctr, L3.ctr, ..., L`iv'.ctr)
As you can see, my data are time series and `iv' represents the window (e.g. 5, 10 or 200 days)
The only problem is that you cannot pass a varli...

1

votes

1

answer

663

Views

### egen and group when data has missing values

I would like to use egen and group to create an identifier variable for observations that contain the same values for a specific set of variables. However, some of the variables contain missing data, resulting in the corresponding identifier having a missing value. Is there a way to get around thi...

1

votes

1

answer

82

Views

### How to create locals on the fly in Stata?

I am trying to create locals on the fly and check them by assigning values to a new variable
gen sampleVar =.
foreach i in AK AL AR AZ {
su income if (year==2012 & state_nsw == '`i''), meanonly
local val_`i' = r(mean)
display '`val_`i'''
}
// check the local
recode sampleVar .= '`val_AL''
// t...