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
0
votes
1 answer

Create expandable varlist

I tried to convert two data files into a matrix in Stata. In the first data file there are only 10 columns, so I used: mkmat d1 d2 d3 d4 d5 d6 d7 d8 d9 d10, matrix(dataname) However, the second data file contains more than 100 columns. Do I have…
Condo_programmer
  • 226
  • 1
  • 4
  • 12
0
votes
1 answer

Year to month date conversion

I have a dataset in wide format that has quarterly counts of an event from Q1 1996 to Q4 2016. The variable names for each quarter are as follows: Q1 = yyyy0101_yyyy0401 Q2 = yyyy0401_yyyy0701 Q3 = yyyy0701_yyyy1001 Q4 = yyyy1001_yyyy0101 I have…
EconoQ
  • 13
  • 6
0
votes
1 answer

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 =…
Kany
  • 39
  • 7
0
votes
2 answers

Macro lost after reading in new file

Using Stata, I define a local macro (macro_name) as a variable (macro_variable) in one data file. After reading in a new file (in the same do file), I'm no longer able to reference that macro. Instead, I receive the error: . di…
kathystehl
  • 831
  • 1
  • 9
  • 26
0
votes
1 answer

Find max of specific list of columns from macro

I have declared a local macro that corresponds to several variable names in my Stata project: local letters a b c d I want to be able to generate a new variable using all variables in the macro letters: gen highest_letter = max(`letters') However,…
oli5679
  • 1,709
  • 1
  • 22
  • 34
0
votes
1 answer

Invalid '`' error when using local macro

I am following instructions from this link on how to append Stata files via a foreach loop. I think that it's pretty straightforward. However, when I try to refer to each f in datafiles in my foreach loop, I receive the error: invalid ` I've set…
kathystehl
  • 831
  • 1
  • 9
  • 26
0
votes
1 answer

Use of local macro

I want to write six temp data files from my original data keeping the following variables: temp1: v1-v18 temp2: v1-v5 v19-v31 temp3: v1-v5 v32-v44 temp4: v1-v5 v45-v57 temp5: v1-v5 v58-v70 temp6: v1-v5 v71-v84 I have tried the…
DirkSa
  • 5
  • 1
0
votes
1 answer

How to assign the maximum amount of strings to macro automatically?

My question's title may be a little bit ambiguous. Previously, I wanted to "acquire complete list of subdirs" and then read the files in these subdirs into Stata (see this post and this post). Thanks to @Roberto Ferrer's great suggestion, I almost…
zlqs1985
  • 509
  • 2
  • 8
  • 25
0
votes
2 answers

How to acquire complete list of subdirs (including subdirs of subdirs)?

I have thousands of city folders (for example city1, city2, and so on, but in reality named like NewYork, Boston, etc.). Each folder further contains two subfolders: land and house. So the directory structure is like: current dictionary ----…
zlqs1985
  • 509
  • 2
  • 8
  • 25
0
votes
1 answer

How to tokenize a extended macro (local :dir )?

I know my title is confusing in the sense that the tokenize command is specified to a string. I have many folders that contain massive, separated, ill-named Excel files (most of them are scraped from ahe website). It's inconvenient to select them…
zlqs1985
  • 509
  • 2
  • 8
  • 25
0
votes
1 answer

How do I loop over part of a variable name?

I need to use a local macro to loop over part of a variable name in Stata. Here is what I tried to do: local phth mep mibp mbp tab lod_`phth'_BL Stata will not recognize the entire variable name. variable lod_mep not found r(111); If I remove the…
pakalla
  • 163
  • 4
  • 10
0
votes
1 answer

Analysis by group and extracting value from a variable for printing

I am doing a little by-group processing, running some regressions by school. What I would like to do is customize my output somewhat so I can see what output belongs to which school. However, I can't seem to process the foreach or the forvalues in…
blue and grey
  • 393
  • 7
  • 21
0
votes
5 answers

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 …
ageil
  • 171
  • 1
  • 3
  • 16
0
votes
1 answer

Stopping at the variable before a specified variable in a varlist

I'm stuck on a tricky data management question, which I need to do in Stata. I'm using version 13.1. I have more than 40 datasets I need to work on using a subset of variables that is different in each dataset. I can't include the data or specific…
PSR
  • 197
  • 1
  • 7
0
votes
1 answer

How can I change the value stored in local macro from string to numeric?

I want to generate a variable with lagged year depending on the year stored in the "$S_DATE" macro. I have stored the year in a macro named date: . local date substr("$S_DATE",8,.) . display `date' 2015 And I want to generate the new variable…
leoxnv
  • 13
  • 1
  • 4