Statalist The Stata Listserver

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

st: Generating blank observations

From   "Patrick Woodburn" <>
To   <>
Subject   st: Generating blank observations
Date   Wed, 8 Nov 2006 12:44:11 +0300

Dear Statalist,
This is my first post to the list, and I hope it is clear enough.  I
just have one question for now:
If I have an id variable called "var1" with a selection of unique values
in a given range of integers (eg the values 1, 3, 5, 6, 7, and 9), and I
want to create new observations which contain each missing value in that
range and are blank for all other variables (eg new observations
containing 2, 4, 8 and 10) and a new variable to flag that they have
been artificially generated, what do I do?  Currently, all I can think
of is the rather roundabout way of doing it below, but I can't help but
think that surely there must be a more efficient method.

Best regards,

*Code begins (dataset already open)

keep var1
drop if var1==.
bysort var1: assert _n==1
gen flag=0
gen id=1
reshape wide flag, i(id) j(var1)
forvalues i=1/10 {
    cap gen flag`i'=1
reshape long flag, i(id) j(var1)
drop id
keep if flag==1
save var1skippedvalues
append using var1skippedvalues
This message (and any associated files) is intended only for the use of 
the individual or entity to which it is addressed and may contain 
information that is confidential, proprietary, subject to copyright or 
constitutes legally privileged information. If you are not the intended 
recipient you are hereby notified that any dissemination, copying, printing 
or distribution of this message, or files associated with this message, 
is Illegal. If you have received this message in error, please notify 
us immediately by replying to the message and deleting it from your 
computer. Medical Research Council deserves the right to monitor all 
communications through its networks. Any views expressed in this message are 
those of the individual sender, except where the message states 
otherwise and the sender to state them to be the views of any such entity.

*   For searches and help try:

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