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]

st: RE: generate new variables using the first record of the group (in statalist-digest V4 #5050)


From   "Allan Reese (Cefas)" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: RE: generate new variables using the first record of the group (in statalist-digest V4 #5050)
Date   Wed, 30 Oct 2013 10:26:33 +0000

>How do I generate a new variable with its value for all observations equals to the first observation or the nth observation of >another variable? [in groups according to the subj]

Stata has a system variable to number cases. If the data are sorted, this can be restarted in each group. Having flagged the nth observation in each group, you can copy it. If there are more than one groups, you then have to split off each value, and maybe someone can think of another way to spread each selected value across all obs - if that is what is wanted.

   sort groupvar /* if not already done */
   by groupvar: gen rowno = _n
   gen chosenval = x if rowno == 1  /* or any other value */
   separate  chosenval, by(groupvar) g(cv)
   egen allcv1 = max(cv1)  /* etc, or might be automated in a loop */ 

Allan

 



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


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