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: Inserting non blank observations multiple times


From   "Lacy,Michael" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Inserting non blank observations multiple times
Date   Sat, 9 Jul 2011 14:52:36 +0000

>
>Date: Fri, 8 Jul 2011 14:48:11 -0400
>From: Bryan Stuart <[email protected]>
>Subject: st: Inserting non blank observations multiple times
>
>I have a data set with 94 observations. After each observation, I want
>to enter 761 (non blank) observations. The new observations will be
>the same for each of the original 94 observations. I found the INSOB
>module, which can easily enter blank observations. This isn't what I
>want. I want to move from something like:
>
>original1
>original2
>original3
>
>to something like:
>
>original1
>new1
>new2
>...
>new761
>original2
>new1
>new2
>...
>new761
>original3
>...
>
>Does anyone have suggestions? Thanks in advance.
>

Here's another simple approach.

// Create the file to insert.
clear
tempfile insert
set obs 5 // would be 761, but small  for demo. 
gen byte insert = 1
gen x = runiform()
gen int order = _n  // perhaps will want to keep these cases in order
save `insert'
//
// Create original data file.
clear
set obs 3 // would be 94, but small for demo. 
gen int temp_id = _n
gen x = runiform()
gen byte insert = 0
//
// ************************************************
// Now, do the work, just one loop and a sort. 
// Include the insert file once for each original case.
forval i = 1/`=_N'  {
   append using `insert' 
   qui replace temp_id = `i' if missing(temp_id)
}
sort temp_id insert order // puts inserted cases into the right place.
//
list


Regards,

Mike Lacy
Assoc. Prof./Dir. Grad. Studies
Dept. of Sociology
Colorado State University
Fort Collins CO 80523-1784
970.491.6721 (voice)

*
*   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