how to delete a previous command

#1
Hi there!

I am new to STATA and I can use the basic command.
However something that I cannot absolutely understand is how to delete a previous command.
Suppose I have deleted some data doing "keep if male == 0". Now all the data available will be only for female -as 0 means female-.
What though if I want to delete the command "keep if male == 0" and come back to the full data set for both female and male?
I have tried to look for it on the internet and I did find something but it did not work out.

I really need your help...!

Thanks a lot!
belfagor
 

bukharin

RoboStataRaptor
#2
You can't undelete data, but you can keep a copy of the dataset before dropping values (eg -preserve-, -tempfile- or -snapshot-). However, such tricks are not usually necessary unless you're doing something relatively complicated. What exactly are you trying to do with your data?
 
#3
Bukharin, thanks a lot for your prompt answer :)

What exactly are you trying to do with your data?
I have a large data set, given to me by my supervisor, something like 500.000 observations. And i have to work on this data to extrapolate information.

But what you have answered me, surprises me.
Suppose -which is actually want I want to do- I want to work only on a small bit of these data: I want just work with data from people who are in the bottom quantile of the monthly consumption distribution.
I need this as I want to see if female participation to school within the bottom quantile monthly cons distribution differs significantly from that one in the higher monthly consumption distribution.
Once I know this, I want to come back to the full data set so that I can repeat the same thing for the top quantile monthly consumption distribution.
I thought -and maybe someone told me- there was a command through which you could simply delete the previous command. Something like "ok, now that I have seen what happens for this part of the distribution, let's come back to the full data set".
Otherwise what I should do?
What I have done so far is that once I have kept only the poorer people, since I could not come back to the full data set I just quit Stata and then opened it again. But since it is quite a pain to do this every time, I was just hoping to find some command which let me have again the full data set.

...is there?:confused:
 

bukharin

RoboStataRaptor
#4
I'm not sure you really need to delete and undelete observations for that... why not just use -if-, eg:
Code:
mean participation if male==0
mean participation if male==1
It's also very much worth learning how to use -by- (see the user's manual).

If there are several steps involved and you don't want to keep typing "if" then -preserve- and -restore- are probably the best bet:

Code:
preserve
keep if male==0
... do whatever you need...
restore

preserve
keep if male==1
... do the same thing...
restore
If you posted your actual code we may be able to figure out something more efficient, but the above should at least do what you need.
 
#5
Bucharin: Thank you very much indeed!

Yeah, that was exactly the command I was looking for! I have just tried it out and works perfectly.
Amazing! Now I can simply can back to the original sample once I have finished looking at only a small part of it.

I am wondering now:
can this preserve command works at different level?
I mean:
when I work with poor ones I may also want to keep only the ones who attend to secondary school.
Something like:

preserve
keep if monthly_consumption < = 3200

keep if level_of_schooling == 10 (which means secondary)

If I then type restore it brings me back to the sample before the first Keep command and I have seen that if I try to write another preserve before the second keep it does not work.
Is there any command to go back to the full data set before the second keep?

Thanks a lot!!!
:)
 

bukharin

RoboStataRaptor
#6
No, you can't do nested preserves. You will need to use one of the other solutions I mentioned in my first post (-tempfile- or -snapshot-). But a better option would be to post the code you're using so that we can take a look and see if there's a more efficient way of doing things.
 

Dason

Ambassador to the humans
#8
Yup. For example if I was to post some R code (which I do regularly) I might write something like this:
HTML:
[code]
plot(density(rnorm(1000,7,10)))
[/code]
inside of my post. I wrapped that bit in HTML tags so that it wouldn't interpret the CODE tags. If I don't wrap it in HTML tags then it appears like this:
Code:
plot(density(rnorm(1000,7,10)))
You can 'quote' what I wrote to see what I'm talking about if it helps.