# Questions tagged [stata]

1172 questions

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...

1

votes

1

answer

45

Views

### Inputting Comma Delimited Data from a .txt when Variables that are Entirely Empty Need to be Read in as Strings

I have some .txt files of delimited data. I first read each one in, save it to a temporary file, and then append the temporary files to another to create the entire data set.
The problem is that some variables in some of the .txt files are blank whereas they are strings in some of the other. When...

1

votes

1

answer

1.6k

Views

### Stata Weighted Least Squares Regression

I realize this may be a novice question, but I'm still a beginner with Stata. I have a dataset that I'm trying to run a weighted least squares regression on, and I've installed the wls0 package. However, when I try to run my regression, I get a strange error:
option wvars() required
r(198);
This d...

1

votes

1

answer

433

Views

### Using user-written command chest in Stata for change-in-estimate effects

I'm using the user-written command chest in Stata to look at the change-in-estimate with the variables in my model.
After running the linear regression of
regress age allelecount gender htn_g dm_g lipid_g i.hx_smoking b_bmi hx_med_asa if cadhx2==0
I run the chest command
chest allelecount, backward...

1

votes

1

answer

1.3k

Views

### Drawing a complicated bar graph in Stata

I am trying to draw a bar graph with a bar for each separate variable on the y axis and then split them according to a category variable.
So, my variables to put in the x-axis are:
phys
emo
soc
sch
They are all scores from 0 to 100 which show quality of life and I want to split each of these on the...

1

votes

1

answer

523

Views

### Maximum likelihood programming in Stata

I am trying to learn ml programming in Stata. As a part of this I am running a program myprobit (the code is adopted from Maximum likelihood estimation with Stata by Gould, Pitblado, and Sribney).
capture program drop myprobit
program define myprobit
args todo b lnf g negH g1
tempvar xb lj
mleval `...

1

votes

2

answer

190

Views

### stata - variable operations conditional to existent vars and to a list of varnames

I have this problem.
My dataset has variables like:
sec20_var1 sec22_var1 sec30_var1
sec20_var2 sec22_var2 sec30_var2 sec31_var2
(~102 sectors, ~60 variables, not all of the cominations are complete or even existent)
My intention is to build an indicator that do an average of variables within se...

1

votes

1

answer

434

Views

### Stata: estadd-weighted dependant var mean (ysumm)

I want to add a row for listing the weighted mean of the dependent variable at the bottom of a regression table. Normally, I would run
reg y x1 x2 x3
estadd ysumm, mean
eststo r1
esttab r1 using results.tex, replace label title('Title') long nomtitles cells('b(fmt(a3) star)' t(par fmt(2))) stats(r2...

1

votes

3

answer

973

Views

### Stata: Efficient way to replace numerical values with string values

I have code that currently looks like this:
replace fname = 'JACK' if id==103
replace lname = 'MARTIN' if id==103
replace fname = 'MICHAEL' if id==104
replace lname = 'JOHNSON' if id==104
And it goes on for multiple pages like this, replacing an ID name with a first and last name string. I was wond...