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
2 answers

How can I name several graphics in an iterative form?

I have the following data structure: id id_name date observation 1 aa 2000 1 1 aa 2001 2 1 aa 2002 1 2 dd 2000 1.5 2 dd 2001 0 2 dd 2002 1 I generate several line plots using the code…
hllspwn
  • 35
  • 4
0
votes
2 answers

Error when using foreach to cut out sample

I want to use foreach to cut out same sample using Stata. I have written the following code snippet: foreach i of numlist 0/11 { preserve keep id projectno nickname gen start=`i'*30000+1 gen end=(`i'+1)*30000 outsheet using…
0
votes
3 answers

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…
Ataullah
  • 31
  • 7
0
votes
2 answers

Generating values for columns between a range

I have the following dataset A B begin_yr end_yr asset brown 2007 2010 asset blue 2008 2008 basics caramel 2015 2015 cows dork 2004 2006 I want A and B to have rows for…
JodeCharger100
  • 903
  • 1
  • 12
  • 25
0
votes
1 answer

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)…
Malick
  • 6,252
  • 2
  • 46
  • 59
0
votes
1 answer

Accessing max/min of a list of numbers stored in numlist / local

Is there a way to access the maximum and minimum values stored in a numlist or local using something like min() and max() in Stata? I want to archive roughly something similar to this: local test 5 10 25 50 local max_test = max(`test') local…
cg89x
  • 29
  • 4
0
votes
1 answer

How is syntax used in a program?

I have used the program command in Stata quite a few times now. Today, I am trying to teach myself how to create a program that uses input. I've tried looking at the syntax help file and at page 71 of these lecture slides I found on the internet,…
ahorn
  • 217
  • 2
  • 12
0
votes
1 answer

Conflict of global macro and loop

I'm trying to automate the process of running through a bunch of files that are sequentially named, manipulate them all in the same way and then save them. I thought that using a forvalues loop with a global macro would be the best way to do this…
0
votes
2 answers

Track how many new variables have been created by splitting a string

I'm using the split command to split a variable which has multiple strings separated by a semicolon. I would also like to keep track of how many new string variables have been created as a consequence of splitting the original string variable and…
0
votes
1 answer

Syntax error when using count in loop

I am trying to run a loop where I count the total in each file under the variable _merge, and then count certain outcomes of _merge, such as _merge=1 and so on. I then want to calculate percentages by dividing each instance of _merge by the total…
statuser
  • 63
  • 1
  • 6
0
votes
1 answer

Issues with macros and variables when reading multiple CSV files one after another

Here are some reproducible data for one of the csv files: * Example generated by -dataex-. To install: ssc install dataex clear input str27 eventname str10(eventdate scrapedate) byte part float(thpercentile median v7 mean) str5 timestamp int…
ababuji
  • 1,683
  • 2
  • 14
  • 39
0
votes
1 answer

Brace required error in nested loop

I'm working on my first loop with a nested loop in Stata, but can't get the code to run. Here is what I have: *identify anti policies of contiguous states forvalues yr = 1983/1993 { foreach state in Alabama Alaska Arizona Arkansas California…
Chris
  • 3
  • 2
0
votes
1 answer

Appending multiple Excel sheets in a dataset

ORIGINAL QUESTION While importing a large dataset in work, I noticed some unexpected behaviour where Stata appears to "forget" a local macro, when using the append command. This seems all the more strange as it appears to be a phenomenon specific…
SeánMcK
  • 392
  • 3
  • 17
0
votes
1 answer

Looping through folder to generate and save graph for each file

Below is one of my csv files created from dataex for a reproducible example: clear input str32 eventname str10 scrapedate float(average thpercentile v5 v6) "EventName" "2015-12-15" 136.9255 83.2 104.875 148.75 "EventName" "2015-12-16" …
ababuji
  • 1,683
  • 2
  • 14
  • 39
0
votes
1 answer

For loop error (invalid syntax)

I just learned how to use a for loop in Stata. I tried this on my data in order to convert my string formatted variables to numeric ones and then move the new numeric variables right next to the old string formatted variables. But somehow Stata…
Todd
  • 399
  • 3
  • 18