Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: FW: Need help with STATA


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: FW: Need help with STATA
Date   Sun, 10 Jul 2011 14:55:15 +0000

<>
Arihanth:

Welcome to Statalist -- I think you'll find it a great resource, but you'll need to read and follow the FAQ (http://www.stata.com/support/faqs/res/statalist.html) if you hope to get helpful responses.  First, no attachments.  Second, it's helpful to include the code you tried and a data example in your post (as you did), but you need to be more explicit about what you are doing (more on this later).  Third, the code that is run in Stata is not called a macro, Stata reserves that word for a name that is associated with some text (see -help macro-), though I realize that code=macro elsewhere.  Next, you say:

> I need to split this data in the intervals of every 100 ms and create separate worksheet/workbook. Could you please help me with the coding for the same. I tried a few codes with the help of my fellow MSc Economics students but was unsuccessful.


I'm not sure want an 'interval' is here, but from your example code, I think you mean 'variable.'  There is no such thing as a workbook/worksheet in Stata -- there are Stata datasets (.dta format) and snapshots (see -help snapshot-).    Last, it's Stata, not STATA.

In your example code you have:

*****
forval k=2/11 {
    forval i=1/10 {
       gen time`k'=time if time< x`k' & time`i'==.
     }
}
*****
the reason this doesnt work is that on your first pass, the first/outside loop sets k to 2 and then the second/inside loop sets i to 1, which evaluates to:
       gen time2=time if time< x2 & time1==.

then in the second pass, k is still 2 (since the i loop is nested in the k loop, it will run through all elements 1/10 before moving on to k==3) and i is now 2, so now you have 
       gen time2=time if time< x2 & time2==.

but time2 already exists, so you get an error.  You could move the -gen- command to the outside loop and then -replace- time`k' in the inside loop if this is what you want, but I don't think fixing this loop gets you to what you want.  Based on your code example and your description, below is an example of how I interpret what you want, but if it's not right, then you need to be more clear in your description:

You'll need dropmiss (-findit dropmiss-) to run this code.  Also, please read the **notes** in the code below.

**************************BEGIN EXAMPLE -- run in your do-file editor
clear
***this part will input the example dataset you gave us:
input time length
0                           60
0.435                     1506
1.88                         60
2.337                     1514
2.461                     1514
5.428                     60
5.828                     1506
9.393                     60
9.754                     1112
13.89                     60
14.309                    1506
14.431                     1506
14.554                     1506
19.368                     60
19.448                     60
19.768                     1414
19.886                     1414
20.018                     1506
end

*******
loc i = .0100
forval n = 1/9000 {
di "`i'"
if `i' != .0100 loc cond "& time > \`=\`i'-.0100'"
g x`n' = time if time < `i' `cond'
g y`n' = time if time < `i' `cond'

****use -findit- to install dropmiss:
dropmiss, force

**if you really want to create a 
****seperate dataset for each time interval:
cap confirm variable x`n'
if !_rc {
qui ta x`n'
if `r(r)' > 0 {
  preserve
     keep time length *`n'
     sa  "data`n'.dta", replace  //this will a lot of datasets!
  restore
      } //end r(r) loop
    }  // end !_rc loop
 loc i = `i' + .0100
}  //end forval loop
**************************END EXAMPLE
Note that the part that creates the new dataset for each time interval can be removed if this isn't really what you want(I suspect it isn't).  It helps if you tell us why you want to do something like that -- it's likely that someone will have a better solution than needlessly separating your dataset into multiple sub-datasets.

- Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]


On Jul 10, 2011, at 6:03 AM, Vijayakumar, Arihanth wrote:

> Hello,
> 
> I am Arihanth Vijayakumar currently pursuing my MSc. Telecommunications and Information Systems at the University of Essex, United Kingdom.
> 
> I need a macro/code which can help me split a set of data for my dissertation on a particular criteria. The file is actually very large with 399327 entries of which the sample data is given below.
> 
> 
> Time (milliseconds) Length of packet
> 0                           60
> 0.435                     1506
> 1.88                         60
> 2.337                     1514
> 2.461                     1514
> 5.428                     60
> 5.828                     1506
> 9.393                     60
> 9.754                     1112
> 13.89                     60
> 14.309                    1506
> 14.431                     1506
> 14.554                     1506
> 19.368                     60
> 19.448                     60
> 19.768                     1414
> 19.886                     1414
> 20.018                     1506
> 
> The data runs till 9000ms (900seconds). I need to split this data in the intervals of every 100 ms and create separate worksheet/workbook. Could you please help me with the coding for the same. I tried a few codes with the help of my fellow MSc Economics students but was unsuccessful. I would be grateful if you can help me with the same.
> 
> Please find attached the code created for your reference.
> 
> Looking forward for your replies.
> 
> Regards,
> Arihanth
> <stata.txt>




*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index