Questions tagged [stata-macros]

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

votes
1

answer
50

Views

error when looping with macros in the indexing

I am trying to create a dummy variable to identify the next five observations after a selection of cutoffs. The first method in the code below works, but it looks a bit messy and I'd like to be able to adjust the number of observations I'm creating dummies for without typing out the same expression...
Susan
1

votes
1

answer
62

Views

Does SPSS have functions similar to double/compound quotes in Stata?

Take an example when we have a series of 10 categorical variables var1, var2,..., var10 which take values from 1 to 5. We create 5 dummy variables from each of these variables. For example, from var1 we generate dumvar1_1,..., dumvar1_5. A dummy will receive the value of 1 if the original variable h...
NonSleeper
1

votes
1

answer
52

Views

Using local macros in names of global macros

How can I use local macros in the names of global macros in Stata 14? For example: global test1 = 250 local n = 1 . di $test1 // works 250 . di $test`n' // does not work (should be 250 and not 1) 1
Andreas
1

votes
1

answer
36

Views

How to create the name of a macro programmatically and use it as a macro?

I am using the community-contributed command gvselect that permits to perform best subsets variable selection (a statistical method to select variables). After I used it, I obtain the following result: . return list macros: r(best3) : ' q105capitalisationboursireouvalo q12nombredefemmesauconseil q...
Malick
1

votes
1

answer
140

Views

Assigning a range of values to a macro

I am trying to rename a list of variables using a for loop. My code looks like this: local x 99 00 01 02 03 04 05 06 07 08 09 10 11 12 local k 2 4 6 8 10 12 14 16 18 20 22 24 26 28 local n: word count `x' forvalues i = 1 / `n' { local a : word `i' of `x' local b : word `i' of `k' rename v`b' num`a...
Rodrigo
1

votes
1

answer
32

Views

Create macros from long string

Consider the following string held in a local macro ABC: A1 A2 B3 C2 B5 A3 C9 C4 B1 A7 How can i create a separate local macro (not variable) for each set of elements? I would also like the new local macros to be sorted alphabetically. For example: A = A1 A2 A3 A7 B = B1 B3 B5 C = C2 C4 C9
user11228397
1

votes
1

answer
483

Views

Preserving data more than once

I am writing some code in Stata and I have already used preserve once. However, now I would like to preserve again, without using restore. I know this will give an error message, but does it save up to the new preserve area?
Tommy Atlee
1

votes
1

answer
79

Views

Export regression results with categorical exposure variables

I would like to run several linear regressions using categorical exposure variables and to output the results to an excel sheet. The code below works fine when the exposure is continuous. However, for categorical exposures the code only outputs the first row of results rather for ever level of the...
aelhak
1

votes
1

answer
137

Views

How to include interaction term in global macro for use in the reghdfe command?

I want to create an interaction term in the community-contributed regdhdfe command. One of the two interaction variables is a continuous variable and used with a global. The other variable is a gender dummy. My code looks something like this: clear all set obs 25000 local vlist v0 v1 v2 v3 v4 v5 v...
Timo K
1

votes
1

answer
40

Views

Passing multiple lists of unspecified length to a program

I would like to create a Stata program that takes multiple lists of unspecified length as arguments. However, I don't know how the program can distinguish between the lists once they're passed in. For example, I'd like to be able to do something like: prog myprog args list1 list2 {something with lis...
Dillon Bowen
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'...
Susie
1

votes
1

answer
161

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

votes
1

answer
166

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

votes
2

answer
41

Views

Extract regression results with names of covariates

I would like to run linear regressions using a categorical exposure variable and to output the results to an excel sheet with the names of each covariate included next to their results. The Stata code below works fine to export the results: sysuse auto.dta, clear describe summ postfile temp str40...
aelhak
1

votes
1

answer
1.9k

Views

“word # of string” idiom (part of extended string functions)

I'm trying to label variables in a forvalues loop on Stata version 11.2. However, I can't get the extended string function word # of string idiom to work: local names `''Growth' 'Mature' 'All''' forvalues i = 0/2 { local name : word `i' of `names' display '`name'' } This follows a Statalist solution...
Richard Herron
1

votes
2

answer
4.8k

Views

How to store a mean value in a local macro and then save it in another file?

I have a Stata file file1.dta and one of the variables is income. I need to calculate average_income, assign it to a local macro, and store in a different Stata file, New.dta. I have tried the following in a do file: #delimit; clear; set mem 700m; use file1.dta; local average_income = mean income;...
Buras
6

votes
5

answer
27.4k

Views

Tabulate multiple variables with common prefix using a local macro

I have a number of variables whose name begins with the prefix indoor. What comes after indoor is not numeric (that would make everything simpler). I would like a tabulation for each of these variables. My code is the following: local indoor indoor* foreach i of local indoor { tab `i' group, c...
tt1977
0

votes
1

answer
13

Views

Different cases of variable name after append

I have appended multiple files into a single sSata dataset. It has now 335 variable names. Some variable names have casing issues like almirah and ALMIRAH storing the same information from different datasets. I am replacing these variables like this one by one: count if mi(almirah) local first=r(N)...
Ataullah
11

votes
2

answer
349

Views

Equivalent of Stata macros in Python

I am trying to use Python for statistical analysis. In Stata I can define local macros and expand them as necessary: program define reg2 syntax varlist(min=1 max=1), indepvars(string) results(string) if '`results'' == 'y' { reg `varlist' `indepvars' } if '`results'' == 'n' { qui reg `varlist' `inde...
user10162045
6

votes
2

answer
266

Views

How do I create a “macro” for regressors in R?

For long and repeating models I want to create a 'macro' (so called in Stata and there accomplished with global var1 var2 ...) which contains the regressors of the model formula. For example from library(car) lm(income ~ education + prestige, data = Duncan) I want something like: regressors
jay.sf
2

votes
3

answer
447

Views

Macro equivalent of Stata [duplicate]

This question already has an answer here: R equivalent of Stata local or global macros 3 answers Can macros be used in R? I did look another question similar to this one, but couldn't understand it well. Let's say I want to create scalars, each with a different name and content. Basically, what I...
PGupta
3

votes
4

answer
1.4k

Views

R equivalent of Stata's for-loop over local macro list of stubnames

I'm a Stata user that's transitioning to R and there's one Stata crutch that I find hard to give up. This is because I don't know how to do the equivalent with R's 'apply' functions. In Stata, I often generate a local macro list of stubnames and then loop over that list, calling on variables whose...
esiotrot
2

votes
1

answer
693

Views

Comparing elements of two different local macros

I have a local macro called peer_list that contains 280 distinct elements, all of which are strings. I also have another local macro called used_list that contains a subset of the elements contained in the local peer_list. For each element in peer_list I would like to test whether that element is...
Parul Sharma
2

votes
1

answer
1k

Views

When are global macros deleted?

I have a do file from which I would like to select a few lines of code and then run them using the Execute (do) button. The problem is that there are a bunch of local macros defined in this do file. I can run them, but then in the Stata command prompt the local macros are not accessible. Thus, I th...
bill999
2

votes
3

answer
5.5k

Views

Storing values in a macro variable

I'm using the levelsof command to identify unique values of a variable and stick them into a macro. Then later on I'd like to use those values in the macro to select records from another dataset that I'll load. What i have in mind is something along the following lines: keep if inlist(variable, '...
aesir
3

votes
1

answer
57

Views

Creating a loop using local macros fails

I tried creating a forvalues loop using local macros: local open forval i=1/10 { local close } `open' di `i' `close' However, I get the following error: forval command may not result from a macro expansion interactively or in do files r(198); What am I doing wrong?
user10279151
4

votes
5

answer
21.4k

Views

How to rename many variables with string suffixes

In Stata, I have a set of variables that all begin with pkg. In their current state, their endings are numeric: pkg1, pkg2, pkg3, pkg4 and so on. I need to change all of these variables' endings to strings: pkgmz, pkggmz, pkgsp, pkgsptc etc. I have a column of these string endings, which I can de...
ben
2

votes
2

answer
1k

Views

How to manipulate filenames with a macro

I want to save the results of my Stata forvalues loop into individual files. One component of the filename should be the value j assigned to the macro within a forvalues loop. Apparently my code leads to an instruction always to save with 1995. As such, I get messages telling me that this file alre...
user2576879
1

votes
2

answer
166

Views

Automated labeling of logarithmic plots

I would like to automate the graph axis values for a series of plots in Stata 13. In particular, I would like to show axis labels like 10^-1, 0, 10^1, 10^2 etc. by feeding the axis options macros. The solution in the following blog post gives a good starting point: Labeling logarithmic axes in Stat...
Glenn Magerman
2

votes
1

answer
140

Views

Append local macro in Stata

In Stata, I want to explore regressions with many combinations of different dependent and independent variables. For this, I decided to use a loop that does all these regressions, and then saves the relevant results (coefficients, R2, etc.) in a matrix in a concise and convenient form. For this matr...
Marie. P.
1

votes
2

answer
84

Views

Keep a list of variables when some variables don't exist

For several countries, I have one dataset and I want to keep a list of variables (specified in a global vlist) for each of them. Some variables do not exist in some countries. I want Stata to ignore the non-existence of these variables and execute the keep command for the rest of the variables. How...
Timo K
2

votes
4

answer
284

Views

What types of languages allow programmatic creation of variable names?

This question comes purely out of intellectual curiosity. Having browsed the Python section relatively often, I've seen a number of questions similar to this, where someone is asking for a programmatic way to define global variables. Some of them are aware of the pitfalls of exec, others aren't. H...
Wilduck
1

votes
4

answer
483

Views

Using regular expressions or subinstr() to clean local macros

My aim is to clean a given local from _ and all numbers following the underscore at the end of the words. Assume that I have underscores followed by numbers at the end of the words only. By using subinstr(), I am able to specify that I want to eliminate _1 (and possibly loop over different numbers),...
Stezzo
2

votes
1

answer
52

Views

Is it possible to make Stata throw an error by default when a global macro is not defined, instead of a missing string?

A feature of Stata that is sometimes inconvenient is that calling a non-defined macro returns the missing value . [edit: Stata actually returns a missing string '', not a numerical missing value], instead of throwing an error. A piece of code, whose correct execution requires the definition of the...
Giuseppe
1

votes
5

answer
131

Views

Replace a subset of data with a sequence of integers

I have a dataset that looks like the following: id test 1 A 2 A 3 A . B . B . B I would like to fill in the missing values with a sequence of integers corresponding to their id: id test 1 A 2 A 3 A 1 B 2 B 3 B I initially thought about using a fo...
ageil
2

votes
1

answer
173

Views

How to separate Stata macro `varlist' with commas for using in mi( ) and inlist( )?

I want to store a list of variables in a macro and then call that macro inside a mi() statement. The original application is for a programme that uses data I cannot bring online for secrecy reasons, and which will include the following statement: generate u = cond(mi(`vars'),., runiform(0,1)) The i...
Kenji
2

votes
1

answer
85

Views

Bring R list into Stata as macro?

I wish to run a Lasso model in R from Stata and then bring a resulting character list (the names of the subset coefficients) back into Stata as a macro (for example, a global). At the moment I am aware of two options: I save a dta file and run an R script from Stata using shell: shell $Rloc --vanil...
Leah Bevis
4

votes
1

answer
3.5k

Views

How to keep a list of variables given some of them may not exist?

I have 100 dta files. I have a list of variables that I need to keep and save temporary copies on the fly. Some variables may or may not exist in a certain dta. I need Stata to keep all variables that exist in a dta and ignore those that do not exist. The following code has wrong syntax, but it co...
CHEBURASHKA

View additional questions