# Questions tagged [stata]

790 questions

1

votes

1

answer

115

Views

### Customized table with multiple columns

I am using the community-contributed command estout to output a customized table from Stata to a latex .tex file. However, I do not know how I can add multiple columns in one table.
Below is a simplified example where I create two separate tables, each containing the standard deviations of the resi...

1

votes

1

answer

67

Views

### ID matching in panel data

My data is organized in the following way:
ID Year ID2012 var1 var2
1 2012 .
A 2014 1
A 2016 .
2 2012 .
B 2014 2
B 2016 .
For year 2014, I know the correspondence between ID2012 and ID2014, and I know that ID2016 is coded in the same way as ID2014. Now I wan...

0

votes

0

answer

4

Views

### Loop strmatch, then dataset intersection, then extract data subset by observation

Step 1: Find intersection between two datasets. I would like to find the intersection between biglist and matchlist.
use matchlist, clear //matchlist contains unique observations that I need
levelsof countryname, local(country1)
use biglist, clear //big list has a lot of duplicates and things I don'...

1

votes

1

answer

127

Views

### How do I reference a data file with a macro?

I have various Stata data files. These are located in different folders. I also have a single do file that uses these files, one at a time.
Is there a way to use a macro to reference a particular dataset in my do file?
For example:
local datafile = "C:\filepath\mydata.dta"
The idea is to use this l...

1

votes

1

answer

141

Views

### Adding time in a file name

Consider the code snippet below:
local date: display %td_CCYY_NN_DD date(c(current_date), "DMY")
local date_string = subinstr(trim("`date'"), " " , "", .)
save "`date_string'_example", replace
mkdir "`date_string'_example"
This creates output as follows:
20170521_example.dta
However, I want to creat...

0

votes

0

answer

4

Views

### How do I plot coefficients of multiple plots in one plot using coefplot in STATA

I have models that have different DVs, but using the same IV, with two different types of identification strategies. In short I have:
Group1
model 1
model 2
model 3
model 4
Group2
model 1-2
model 2-2
model 3-2
model 4-2
I want to plot the coefficients where I can compare coefficient estimate of the...

1

votes

1

answer

1.3k

Views

### Convert odd Stata string variable to date

I currently have economic data in the format YYYY.QX where Q indicates "Quarter" followed by X, which is in [1,4]. This is interpreted as a string.
I've tried to use the date(series, "YMD") and formatting command, as well as the encode function.
Ideally, I'd end up with a numerical variable indicati...

1

votes

1

answer

692

Views

### Save stata file as R file [closed]

I am trying to save the stata file as a R file using the "save command" but repeatedly getting an error:
>library(foreign)
# read stata file (there is a stata file "data.dta" in working directory)
>read.dta("data.dta")
var1 var2 var3 var4 var5
1 -4.239497 0.2406101 3.786384 -...

1

votes

1

answer

1.8k

Views

### Stata: removing line feed control characters

I have a dataset which I export with command outsheet into a csv-file. There are some rows which breaks line at a certain place. Using a hexadecimal editor I could recognize the control character for line feed "0a" in the record. The value of the variable producing the line break shows visually (in...

1

votes

1

answer

806

Views

### Does the equivalent of matlab's xlswrite() exist for Stata?

I need to export several matrices created in Stata to several different specifically named sheets of an already existing excel file. This would be a piece of cake in Matlab using xlswrite(). I am having trouble finding a similar command in Stata.
"xml_tab" would work but it doesn't seem to want to...

1

votes

2

answer

1.5k

Views

### Reorder Stata variable with respect to another variable

I have these data:
Node Time Reorder Node
201 8:00:00 395
395 8:10:00 405
280 7:50:00 280
405 8:30:00 201
I need to copy the vector called "Time" and reorder it so that the relationship between node number and time in the first two columns is preserved. In other words, I want...

1

votes

1

answer

637

Views

### Using * to specify variables in Stata loop does not work

Why does this code not work in Stata? The error it gives me is that:
* invalid name
However, when I use
sexd1 sexd2
instead of
sexd*
it works fine. sexd* works fine as a local when I am typing commands in Stata's command box.
Here is the code:
local list_of_variables weight midpoint_hhinc...

1

votes

2

answer

199

Views

### Using if () with shorter syntax

Is there a way to express this with a terser syntax
if (`avgInc' > 0) {
loc avgIncDec = "increased"
}
else {
loc acgIncDec = "decreased"
}
Something like an InlineIf (iif):
loc avgInc = iif((`avgInc' > 0), "increased", "decreased")

1

votes

2

answer

4.1k

Views

### “too many variables specified” error with predict following logit

I have a panel of data (firm-years) that span several countries. For each country I estimate a logit model using the first five years then I use this model to predict probabilities in subsequent years. I foreach loop over the countries and forvalues loop over the subsequent years.
The first few coun...

1

votes

1

answer

958

Views

### Reverse loop fails using forvalues syntax

Why can't I use the forvalues syntax to loop in reverse? I can use foreach of numlist syntax to loop in reverse, but I'm confused why the forvalues examples below fail silently while the equivalent foreach of numlist examples work.
* both fail silently
forvalues j = 5/4 {
display `j'
}
forvalues j =...

1

votes

2

answer

143

Views

### Stata - inputting data from .txt with “” and ,

I am using perl to scrape the following through .txt which I'd ultimately bring into Stata. What format option works? I have many such observations, so would like to use an approach over which I can generalize.
The original data are of the form:
First Name: Allen
Last Name: Von Schmidt
Birth Year:...

1

votes

2

answer

1.9k

Views

### How to remove all observations before this one in Stata?

Using Stata, I would like to drop all observations that come prior to the current one if a certain criterion is met.
To be exact, in this sample I would like to drop all observations that appear before an observation with a close of 0.
t close id rdate
MPC 0 MPC 31may2011
MPC 0 MPC...

1

votes

1

answer

5.6k

Views

### How to add a common prefix to all variables in Stata 12

I have 100 variables in a .dta file and I would like to add a prefix "1" before each variable name. Looking at the documentation it looks like I have to use =. but that requires to specify which variables. How can I use that for all variables? Can anyone help me out? (Also the command "renpfix" has...

1

votes

2

answer

3k

Views

### What command in Stata 12 do I use to interpret the coefficients of the Limited Dependent Variable model?

I am running the following code:
oprobit var1 var2 var3 var4 var5 var2##var3 var4##var5 var6 var7 etc.
Without the interaction terms I could have used the following code to interpret the coefficients:
mfx compute, predict(outcome(2))
[for outcome equaling 2 (in total I have 4 outcomes)]
But since mf...

1

votes

1

answer

767

Views

### Stata. How to match values in 1:m relationship?

I have two data sets. First one is:
countyGroup income other_data_
1 20990 …
2 25622 …
3 24289 …
4 27391 …
5 23326 …
6 19261 …
7 15197 …
8 11132 …
The second one is:
countyGroup state county other_data
1 IL...

1

votes

1

answer

1.1k

Views

### running program inside a loop in Stata

I am trying to run a program inside a loop, but I am getting
-Break--
r(1);
Here is my script:
clear all
forvalues i=1/5{
sysuse auto if rep78==`i'
capture program drop testpro
program define testpro,eclass
preserve
tabstat price mpg headroom,stat(mean) save
mat avg=r(StatTotal)
ereturn post avg
r...

1

votes

1

answer

4.9k

Views

### Block bootstrap with indicator variable for each block

I want to run block bootstrap, where the blocks are countries, and include country indicator variables. I thought the following would work.
regress mvalue kstock i.country, vce(bootstrap, cluster(country))
But I get the following error.
. regress mvalue kstock i.country, vce(bootstrap, cluster(count...

1

votes

1

answer

71

Views

### Stata 12 - A Long Tab and Getting Past the “More” Automatically

If I tab for values in Stata 12 and there is a long list of values, Stata 12 seems to pause and display More. It seems to want me to press Return (or perhaps any value) in order to continue. I did not notice this feature with earlier features of Stata, say Stata 11, meaning that when I would hit t...

1

votes

1

answer

125

Views

### What commands in Stata are directly compiled from C?

The answer would be helpful in identifying the bottlenecks in the code.
Inspired by this (URL updated): http://www.stata.com/statalist/archive/2012-03/msg01181.html
On Mon, Mar 26, 2012 at 8:10 PM, Nick Cox wrote:
>
-tabulate- is a built-in command, namely compiled C code. If you want
to look at th...

1

votes

2

answer

2.6k

Views

### Adding variables in Stata and then dividing by a number is giving unexpected results

I am trying to calculate z-scores by creating a variable D from 3 other variables, namely A, B, and C. I am trying to generate D as : D= (A-B)/C but for some reason when I do it, it produces very large numbers. When I did just (A-B) it did not get what it should have when I calculated by hand, inste...

1

votes

2

answer

1.5k

Views

### Bring value labels to variable labels when reshaping wide

In Stata, I would like to be able to bring value labels up into variable labels when reshaping wide.
My raw data looks like this:
patient hosp_id hosp_name charges
Andrew 1 Springfield General $10
Barry 1 Springfield General $20
Crista 2...

1

votes

1

answer

8.1k

Views

### Stata: Using egen, anycount() when values vary for each observation

Each observation in my data presents a player who follows some random pattern. Variables move1 up represent on which moves each player was active. I need to count the number of times each player was active:
The data look as follows (with _count representing a variable that I would like to generate)....

1

votes

2

answer

251

Views

### Display data in Stata loop

I have a loop in Stata 12 that looks at each record in a file and if it finds a flag equal to 1 it generates five imputed values. My code looks like this:
forvalues i=1/5 {
gen y3`i' = y2
gen double h`i' = (uniform()*(1-a)+a) if flag==1
replace y3`i' = 1.6*(invibeta(7.2,2.6,h`i')/(1-invibeta(7.2,2.6...

1

votes

1

answer

4.2k

Views

### Define a variable by concatenating a string with a number in a loop of Stata

I am new to programming in Stata. My question is to run several pairs of regressions in a loop, like the following:
reg outcome1 outcome2 covariates
reg outcome2 outcome1 covariates
I tried the following ways, but the first two came with the error "ambiguous abbreviation" and the second the error "t...

1

votes

1

answer

2.1k

Views

### Getting unknown function mean() in a forvalues loop

Getting unknown function mean for this. Can't use egen because it has to be calculated for each value. A little confused.
edu_mov_avg=.
forvalues current_year = 2/133 {
local current_mean = mean(higra) if longitbirthqtr >= current_year - 2 & longitbirthqtr >= current_year + 2
replace edu_mov_avg =...

1

votes

2

answer

88

Views

### Setting Up a Dynamic Stopping Point for a Loop

Data is setup with a bunch of information corresponding to an ID, which can show-up more than once.
ID Data
1 X
1 Y
2 A
2 B
2 Z
3 X
I want a loop that signifies which instance of the ID I am looking at. Is it the first time,...

1

votes

1

answer

678

Views

### Simple Network Analysis with Stata

I have the following dataset that tells me if there is a connection between two people:
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 connected to person_3...

1

votes

1

answer

279

Views

### Stata infile using .dat file with multiple line obs of varying #s of lines separated by string

I have large .dat files that I want to import to Stata. The .dat files are two long columns with multiple lines per observation. I want to make variables out of the different lines. The problem is that I cannot use lrecl(#) because the observations are different numbers of lines in length, and there...

1

votes

1

answer

3.3k

Views

### Stata: tag all values in a group based on a characteristic of any values in the group

I think egen might help me here, but for whatever reason I can't quite figure out the right syntax. I'd like to create a new variable that takes a value of 1 for all observations in a group if, for any of the observations in the group, X is true. So, for example, my data has the obs, group, and flag...

1

votes

3

answer

4.4k

Views

### Arrays / Vector equivalent in Stata

Being familiar with R, I'm wondering what is the Stata equivalent of vectors (e.g. var

1

votes

3

answer

813

Views

### “Capture” command seems to ignore iter(#) option of -xtlogit- using Stata

I am running a .do-file which includes many models estimated by xtlogit. Even if a model does not converge I would like to document this in the log. As the .do-file takes some hours to run, I normally go away and do something else. Unfortunately, the non-convergence of a model stops the whole .do-fi...

1

votes

2

answer

62

Views

### Finding a match among values occurring previously

I have the following data set
time person1_person_2 person2_person1 occurrence cell_count
1 A_B B_A 0 1
2 A_C C_A 0 2
3 B_A A_B 1 3
4 E_A...

1

votes

1

answer

2.7k

Views

### Stata estpost esttab: Generate table with mean of variable split by year and group

I want to create a table in Stata with the estout package to show the mean of a variable split by 2 groups (year and binary indicator) in an efficient way.
I found a solution, which is to split the main variable cash_at into 2 groups by hand through the generation of new variables, e.g. cash_at1 and...

1

votes

2

answer

1.3k

Views

### Stata: replace missing values with the values from observations with same ID number

I am using population-level American Community Survey data to look at factors that affect income from self-employment, with a primary interest in the female population. I want to create a variable to measure "husband's income." There is a variable pincp that measures a person's total income, and dum...

1

votes

2

answer

3.3k

Views

### Stata: code binary variable conditional on key words in string variable

Is there any way to code a binary variable dependent on keywords being present in a given string variable? Simple example:
I have a string variable that describes various meals and a dummy variable that denotes if a given meal is breakfast or not. Is there any way to code
breakfast = 1 if meal== [t...