Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Sample command question

From   "Nick Cox" <>
To   <>
Subject   RE: st: Sample command question
Date   Mon, 30 Oct 2006 02:46:21 -0000

Richard's line of code does what you ask. 
It works on just the variable that you are focusing on. 

I can see some other problems, some trivial, some
not so, that you will face. 

The -foreach- loop over variables needs the keyword -of-, 
not -in-. This detail also was prominent in a thread 
a couple of days ago. 

Despite a loop over variables and another loop over
10 replicates, you are saving to just 10 files. Thus 
only the last set of 10 files will be left in memory. 

Except that I don't think that will happen either. 

As the initialisation -local i = 1- 
takes place outside the outer loop, something else 
will happen. The first time around the inner loop, 
i will end as 10. The second time around the 
inner loop i will end as 11. The inner loop will
then not be entered again. 

The initialisation should probably be dumped and 
the inner loop replaced by -forval i = 1/10 ... 

But then it seems that you intend to produce 910 
files, even though that's not what the code does. 
If that's true, you're setting yourself up with 
a pretty awkward management task. 

I recommend that you get something very very simple 
working before you try to complicate it. 


David McClintick
> Actually, I just realized that your suggestion isn't going to 
> work. As I
> am generating a sample based on each variable, and I have different
> amounts of missing values for each variable, your command 
> would drop all
> observations that had any missing value for any one of the 
> variables. I
> only want to drop observations for that particular sample when missing
> in that particular variable.

> Thanks, not sure why I didn't think of that.

Richard Williams
> At 07:48 PM 10/29/2006, David McClintick wrote:
> >I have the written the following code to run a sample command on each
> >one of my variables:
> >
> >local i=1
> >foreach x in var1-var91 {
> >         while `i' <=10 {
> >                 use c:\ado\personal\easy.dta
> >                 sample 5, count
> >                 save mewsample`i', replace
> >                 local i= `i' +1
> >}
> >}
> >
> >However, I have a problem because there are missing values in each
> list,
> >and my random samples often return a missing value as one of their 5
> >terms. What's the best way to exclude missing values from the sample
> >generation?
> Before the sample command, could you have something like
> keep if !missing(`x')

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index