stata

Nothing to restore error

人盡茶涼 提交于 2019-12-11 06:34:36
问题 I have a do -file that i use to clean-up a Stata dataset and generate descriptive statistics. The first command in this file is preserve , while the last one is restore . The do -file is too long to attach on here but between these two commands, there are others such as generate , replace , tabulate and collapse . The problem is that sometimes i get the following error message: nothing to restore Does anyone know what might be wrong? 回答1: The problem presents itself when you are trying to run

Stata - How to Generate Random Integers

为君一笑 提交于 2019-12-11 06:29:56
问题 I am learning Stata and want to know how to generate random integers (without replacement). If I had 10 total rows, I would want each row to have a unique integer from 1 to 10 assigned to it. In R, one could simply do: sample(1:10, 10) But it seems more difficult to do in Stata. From this Stata page, I saw: generate ui = floor((b-a+1)*runiform() + a) If I substitute a=1 and b=10, I get something close to what I want, but it samples with replacement. After getting that part figured out, how

Creating variable out of conditional values in another one

半城伤御伤魂 提交于 2019-12-11 06:14:00
问题 I have quite a large conflict dataset (71 million observations) with many variables and date (daily). This is from the GDELT project for which the way the dataset is structured is that for each day, there is a target country and a source country of aggression. Namely, the first of January of 2000, many countries engaged in aggressive behaviour against others or themselves, and this dataset tracks this. It looks like this: clear input long date_01 str18 source_01 str19 target_01 str4 cameocode

Random assignment in treatment groups

为君一笑 提交于 2019-12-11 06:08:03
问题 I have 1200 patients who I would like to assign in 4 random treatment groups. I can create the groups as follows: generate groups=1 replace groups=2 if id>300 replace groups=3 if id>600 replace groups=4 if id>900 However, how can I make the assignment of patients in these groups random? 回答1: You simply need to create a random variable to sort on: clear set obs 1200 generate random = runiform() sort random Then you can use the seq() function of the egen command: egen groups = seq(), from(1) to

How do I create a stata local macro in python? [duplicate]

点点圈 提交于 2019-12-11 05:58:17
问题 This question already has answers here : Equivalent of Stata macros in Python (2 answers) Closed 3 months ago . Let me edit my question to clarify. Here is the python code: decade_begin = 1970 while decade_begin <= 1994: for x in range(0, 10): a= decade_begin + x decade_begin = a+1 decade_end=a print "year_",decade_begin,"s" #start year The output of the last line is: year_1980s year_1990s I want to be able to create a variable such that: year_1990s = [a] In stata this is quite easy, using a

How to create locals on the fly in Stata?

夙愿已清 提交于 2019-12-11 05:19:22
问题 I am trying to create locals on the fly and check them by assigning values to a new variable gen sampleVar =. foreach i in AK AL AR AZ { su income if (year==2012 & state_nsw == "`i'"), meanonly local val_`i' = r(mean) display "`val_`i''" } // check the local recode sampleVar .= "`val_AL'" // this is what I get: 5242.57421875 ..... 5352.66796875 . invalid name r(198); // check 2 the local recode sampleVar .= `val_AL' // error ANSWER: My problem was that I tried recode sampleVar .= `val_AL' +

How can I list negative values across my dataset?

孤人 提交于 2019-12-11 04:14:52
问题 I am using a dataset with about 100 variables and 1000 rows, similar to the one below: . var1 var2 var3 var4 AL 10 11 12 13 AK -1 0 0 18 AZ 5 -5 -2 22 VA 15 16 0 0 How can I list the variables / observations that have a negative value? For example, I would like to list that AK has negative var1 and AZ has negative var2 and var3 . 回答1: Here's an example of how you can create a marker variable for each of your var variables: clear input str2 state var1 var2 var3 var4 AL 10 11 12 13 AK -1 0 0 18

Stata random number generator in C(++)

烈酒焚心 提交于 2019-12-11 03:54:59
问题 I am rewriting (and parallelising) some Stata simulations in C++. For testing purposes I would like to use Stata's random numbers. So far my approach has been to generate the numbers in Stata, dump them to a CSV file (100s MB), and then read that in my C++ program. This is slow and inelegant. Ideally I would like to generate the same uniforms with C++ code. I have read that Stata uses the KISS algorithm (cite http://blog.stata.com/tag/random-numbers/). I found a C implementation at http:/

How to efficiently create lag variable using Stata

有些话、适合烂在心里 提交于 2019-12-11 03:41:28
问题 I have panel data ( time: date, name: ticker). I want to create 10 lags for variables x and y . Now I create each lag variable one by one using the following code: by ticker: gen lag1 = x[_n-1] However, this looks messy. Can anyone tell me how can I create lag variables more efficiently, please? Shall I use a loop or does Stata have a more efficient way of handling this kind of problem? 回答1: @Robert has shown you the streamlined way of doing it. For completion, here is the "traditional",

most efficient I/O setup between Stata and Python (Pandas)

a 夏天 提交于 2019-12-11 03:32:03
问题 I am using Stata to process some data, export the data in a csv file and load it in Python using the pandas read_csv function. The problem is that everything is so slow. Exporting from Stata to a csv file takes ages (exporting in the dta Stata format is much faster), and loading the data via read_csv is also very slow. Using the read_stata pandas function is even worse. I wonder is there are any other options? Like exporting a format other than csv? My csv dataset is approx 6-7 Gb large. Any