Questions tagged [stata-macros]

A macro in Stata is comprised of a name and contents. When a properly punctuated macro name appears in a command or script, its contents are expanded and replace the macro name.

Macros in Stata can be distinguished in two flavours: global and local. Both are defined with the commands of the same name. The contents of the former can be expanded anywhere. In contrast, the latter are only available within a program or script where they were defined.

The chapter Macro definition and manipulation in the manual offers more detailed information.

For questions involving the use of macros in the context of Stata, please use the tag in addition to the main tag.

171 questions
2
votes
3 answers

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…
aesir
  • 565
  • 2
  • 13
  • 23
1
vote
1 answer

Passing a Local to a Python program from Stata

I am trying to write a Stata program that passes a local to a Python function. It is based on this example, where this approach works. Here is a toy example demonstrating the problem: python clear capture program drop get_data program get_data …
dimitriy
  • 9,077
  • 2
  • 25
  • 50
1
vote
1 answer

Generating categorical variable

In my Stata data set, the "alternative" variable consists of 4 modes including pier, private, beach and charter. I want to generate new variable y as follows: We collapse the model to three alternatives and order the alternatives, with y = 0 if…
1190
  • 365
  • 2
  • 10
1
vote
1 answer

Creating "placeholder" variables in Stata

There is a series of regressions I want to run, and in each regression, I decide if I want a covariate or not. So for example, let's say I have a local macro with 10 covariates: local covariates "`N' age `N' sex `N' age2 `N' job job2 `N'" I want 10…
1
vote
1 answer

Interaction between a single variable and a macro in Stata

I'm trying to run a regression with a bunch of interactions. Specifically, I have a macro and one interaction variable, but want the interaction variable to interact with everything in the macro. So far, what I have looks like local example…
1
vote
1 answer

Select first N words of a local macro in Stata

Let's say I have the macro: local words = "first second third fourth" I want to select the first 3 words of it. I know that I can select, say the second, the word using: local select: word 2 of `words' di "`select'" However, I would like to have…
1
vote
1 answer

Reshaping when variables are not unique

I have a dataset below and I want to reshape it in Stata. clear input int id str7 item float consumption 1 "food123" 0.72 1 "food174" 0.51 1 "food167" 0.76 1 "food160" 0.02 1 "fruit240" 0.52 1 "fruit263" 0.06 1 "fruit254" …
Amin Karimi
  • 407
  • 2
  • 16
1
vote
1 answer

Copy values from one observation to another based on unique identifier

I have a dataset with many variables. For a number of observations I want to replace their values (in a large number of variables) with the values of another observation (for the same variables) based on a unique identifier. The unique identifier is…
Timo K
  • 29
  • 6
1
vote
1 answer

Is there a simple way of extract the N first words from a local macro which is comma or space+comma separated in Stata?

Given a local macro that contains a string of levels which are separated by either comma (",") or comma and space (", ") or even only space (" "), is there a simple way to extract the first N levels (or words) of this local macro? The string would…
Marcelo Avila
  • 2,314
  • 1
  • 14
  • 22
1
vote
1 answer

Stata if condition within a loop

I am trying to write a loop with a macro variable that will change depending on the iterated value of the loop. See the code below. I want to loop over ticker values "QLD" and "DDM" and i want the local macro index to equal "QQQ" if the ticker is…
Nathan
  • 23
  • 4
1
vote
1 answer

Scalar formatting problem with number of digits

I am trying to write a command that ereturns a scalar that is percentage rounded to 2 decimal places. The percentage can be negative or positive, with unknown number of digits before the decimal point. Here's MRE that shows the problem I am…
dimitriy
  • 9,077
  • 2
  • 25
  • 50
1
vote
3 answers

How can I import specific files?

I am trying to import hundreds of U.S. county xls files together to form a complete dataset in Stata. The problem is that for every county, I have several files for different years, so that my list of file names looks like…
trinitysara
  • 175
  • 1
  • 8
1
vote
1 answer

How to iterate plot over different variables?

I have the following code: hist house1 if house1 >0 & house1 <200000, bin(25) fraction by(Year) graph export house1.png, replace I would like to iterate it substituting house1 with car1 and bed1 without copy-pasting the code and substituting, or…
fedpug
  • 15
  • 3
1
vote
2 answers

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…
aelhak
  • 441
  • 4
  • 14
1
vote
1 answer

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…
Dillon Bowen
  • 346
  • 3
  • 9
1 2
3
11 12