stata-macros

How to extract unique strings from a macro?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 03:14:29
问题 I'm trying to automate a reshape using Stata. I have a series of variables measured yearly. They are all named varname_yy , where yy is a number referring to the year of measurement. I managed to extract all the stubs varname_ from the variables and to put them into a macro using the following code: local stubs foreach var of varlist `myvars' { local stub = substr("`var'",1,length("`var'") - 2) local stubs `stubs' `stub' } The problem is that I end up with many repeated stubs in the stubs

Tabulate multiple variables with common prefix using a local macro

旧街凉风 提交于 2019-12-04 23:29:51
问题 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, col freq exact chi2 } The problem is that indoor in the foreach command resolves to indoor* and not to the list of the indoor questions, as I hoped. For this reason, the tab command is followed by too

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

送分小仙女□ 提交于 2019-12-02 18:09:56
问题 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 ---- city1 ----- house ------ many .xlsx files ----- land ----- city2 ----- city3 ··· ----- city1000 I want to get the complete list of all subdirs and do some manipulation (like import excel ). I know there is a macro extended function: local list: dir

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

China☆狼群 提交于 2019-12-02 15:19:48
问题 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 manually so I need to rely on Stata extended macro function local :dir to read them. My code looks as follows: foreach file of local filelist { import excel "`file'", clear sxpose, clear save "`file'.dta", replace } Such code will generate many new

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

拟墨画扇 提交于 2019-12-02 08:39:41
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 manually so I need to rely on Stata extended macro function local :dir to read them. My code looks as follows: foreach file of local filelist { import excel "`file'", clear sxpose, clear save "`file'.dta", replace } Such code will generate many new dta files and the directory is thus full of these files. I prefer to create a single new data file for

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

蹲街弑〆低调 提交于 2019-12-02 08:00:41
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 ---- city1 ----- house ------ many .xlsx files ----- land ----- city2 ----- city3 ··· ----- city1000 I want to get the complete list of all subdirs and do some manipulation (like import excel ). I know there is a macro extended function: local list: dir to handle this issue, but it seems it can only return the first tier of subdirs, like city_i , rather

How do I reference a data file with a macro?

让人想犯罪 __ 提交于 2019-12-01 23:50:24
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 time. The global definition (without the dta extension) is: global datafile = "C:\filepath\mydata"

R equivalent of Stata local or global macros

穿精又带淫゛_ 提交于 2019-11-30 14:45:23
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 wondering if trying to re-write Stata code line by line in R is not the best way to learn R. Can someone

Equivalent of Stata macros in Python

爱⌒轻易说出口 提交于 2019-11-30 10:55:34
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' `indepvars' } end sysuse auto, clear So instead of: reg2 mpg, indepvars("weight foreign price") results("y") I could do: local options , indepvars(weight foreign price) results(y) reg2 mpg `options' Or even: local vars weight foreign price local options , indepvars(`vars') results(y) reg2 mpg `options'

Equivalent of Stata macros in Python

▼魔方 西西 提交于 2019-11-29 15:57:34
问题 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' `indepvars' } end sysuse auto, clear So instead of: reg2 mpg, indepvars("weight foreign price") results("y") I could do: local options , indepvars(weight foreign price) results(y) reg2 mpg `options' Or