stata-macros

Comparing elements of two different local macros

三世轮回 提交于 2020-01-15 07:29:07
问题 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 in the local used_list . If the element exists in used_list I would like to discard it, otherwise I would like to execute another set of conditions. I have tried to use the following code but it

How do I reference a data file with a macro?

北战南征 提交于 2019-12-31 03:15:14
问题 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 later in the code as follows: use `datafile', clear Defining the macro as a global variable works. But I don't want to make it global, so it doesn't prevent me from running two separate programs at a

R equivalent of Stata local or global macros

ε祈祈猫儿з 提交于 2019-12-30 05:11:22
问题 I am a Stata user trying to learn R. I have a couple of lengthy folder paths which, in my Stata code, I stored as local macros. I have multiple files in both those folders to use in my analysis. I know, in R, I can change the working directory each time I want to refer to a file in one of the folders but it is definitely not a good way to do it. Even if I store the folder paths as strings in R, I can't figure out how to refer to those. For example, in Stata I would use `folder1'. I am

How do I confirm existence of all respective variables of a variable list?

廉价感情. 提交于 2019-12-25 03:11:42
问题 Similar threads have not led me to solve the following problem. I use a local macro to specify a varlist with multiple variables and want to check whether each of the variables in this varlist exists in the dataset I use. So as to get a quick overview which variables do not exist in the dataset. I have tried the following code so far: local vlist caseid midx hidx v000-v013 v016 v021-v025 v101 v102 foreach v of local vlist { capture confirm variable `v' if !_rc { display in red "variable

How can I import specific files?

允我心安 提交于 2019-12-25 01:17:20
问题 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 this: county1-year1970.xls county1-year1975.xls county2-year1960.xls county2-year1990.xls For each county, I only want the file from the most recent year (which varies across counties). So far, I have written code to loop through each possible file name, and if the file

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

☆樱花仙子☆ 提交于 2019-12-23 15:27:25
问题 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 could serve as a good pseudo code to give one a general idea of what should be done: forval j = 1/100 { use data`j' local myVarList ="" foreach i of varlist var1 var2 var3 var4 var5 var6 var7 var8 {

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

喜你入骨 提交于 2019-12-17 06:51:52
问题 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 <- c("education", "prestige") lm(income ~ @regressors, data = Duncan) I could find is this approach. But my application on the regressors won't work: reg = lm(income ~ bquote(y ~ .(regressors)), data =

Loop for changing date format and generating graphs

你说的曾经没有我的故事 提交于 2019-12-13 20:28:54
问题 I would like to loop through a number of csv files in order to change the format of date variables and generate graphs each with a title that is simply the name of the respective file. Here is an example of one of the csv files created using dataex : 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" 130.4555 78.55 99 138.22 "EventName" "2015-12-17" 123.66705 72.7 90.25 131.2 "EventName"

Extracting random integers from a given variable

不问归期 提交于 2019-12-13 03:18:48
问题 My task is picking 3 years randomly within a time interval 500 times for simulation purpose. More specifically,I want to select 3 random years from 2007 to 2016 ( 10 years), for example 2008 , 2012 and 2014 . So it's more or less equivalent to extracting random integers from a variable. My solution is the following: * The following (empty) dataset will be used to append the results of the Monte Carlo simulations use "recession_parms.dta", clear save "ind_simulations.dta", replace forvalues i

What types of languages allow programmatic creation of variable names?

淺唱寂寞╮ 提交于 2019-12-11 03:31:57
问题 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. However, I've recently been programming in Stata, where the following is common: local N = 100 local i = 1 foreach x of varlist x1 - x`N' { local `x' = `i' * `i' ++i } In Stata parlance, a local