stata

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

Stata:Comparison within a variable but between individuals

▼魔方 西西 提交于 2019-12-25 18:02:23
问题 I have a variable ranking which consists of many groups of different sizes which are all ranked. So one group might be 1-6 the next 1-4 and the next 1-52. I know want to create two variables that sum the differences between an individual and all individuals above and below him respectively. For a group of 5 individuals and individual 1 that means I want to get UP: SUM(1-1) =0 DOWN: SUM((1-5)+ (1-4)+ (1-3)+ (1-2)) = -10 回答1: Some considerable guesswork seems needed here. Summing the

Identify unique levels of categorical variable

坚强是说给别人听的谎言 提交于 2019-12-25 16:49:09
问题 I have a list of person ids, and the types of medicines they got on specific dates. I would like to create a variable count whereby I can give the indicator 1 to the first drug that occurs, 2 to the second unique drug and 3 to the third unique drug. When the first drug occurs after the second and third, I want it to still have the indicator 1. Likewise for unique drug 2, it should maintain the value 2 throughout the person's whole medication history, and the same for drug 3. +----------------

Customizing tables in Stata

青春壹個敷衍的年華 提交于 2019-12-25 14:49:57
问题 Using Stata14 on windows, I am wondering how to build customized tables from several regression results. Here is an example. We have reg y, x1 predict resid1, residuals summarize resid1 Which gives: Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- resid1 | 5,708,529 4.83e-11 .7039736 -3.057633 3.256382 And run another regrerssion and similarly obtain the residuals: reg y, x2 predict resid2, residuals I would like to create a table

Customizing tables in Stata

一笑奈何 提交于 2019-12-25 14:49:24
问题 Using Stata14 on windows, I am wondering how to build customized tables from several regression results. Here is an example. We have reg y, x1 predict resid1, residuals summarize resid1 Which gives: Variable | Obs Mean Std. Dev. Min Max -------------+--------------------------------------------------------- resid1 | 5,708,529 4.83e-11 .7039736 -3.057633 3.256382 And run another regrerssion and similarly obtain the residuals: reg y, x2 predict resid2, residuals I would like to create a table

Matching strings that appear in both lower and upper case (or are a combination of lower case and upper case letters)

送分小仙女□ 提交于 2019-12-25 07:48:02
问题 I am trying to find a word from a column in Stata that could appear in all caps or all lower case. foreach varlist_cust in "xyz" "XYZ" "XyZ" { replace cus_tag = strpos(customer_name, "`varlist_cust'") if cus_tag==0 } Is there a more efficient method of conducting this analysis? Maybe with the use of regular expressions? 回答1: No need for regular expressions as Stata has a built in lower() function. See, for example clear input str9 customer_name "Ander2Ed" "sonu" "abcXyZcba" "XYZ" "zXyZ" end

Renaming variables that contain a certain string

六月ゝ 毕业季﹏ 提交于 2019-12-25 06:39:20
问题 I have a list of variables all containing the same string "test". How do I rename all of these variables to for example var1-var20, where 20 is the number of variables. The order is not important here. I tried installing the package "renvars", and did the following renvars *test* \ var1-var20 but this does not work. Any help is appreciated. 回答1: To be any more help we'll need the error and how it fails. *test* should be a valid varlist and if there are the same numbers of variables in each

Store values from a variable and reuse them

跟風遠走 提交于 2019-12-25 04:36:18
问题 This is a question that could help me to solve another, still unsolved question I posted. Basically I need to condition a dataset in Stata and I thought a procedure which would need to first store certain values of a variable in a sort of matrix and then use compare the values of another variable with those stored in the matrix. A simple example could be the following: obs id act1 act2 year act1year 1 1 0 1 2000 0 2 1 1 0 2001 2001 3 1 0 1 2004 0 4 2 1 0 2001 2001 5 2 1 0 2002 2002 6 2 0 1

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