Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Need help for calculation across observations within variable


From   Michael Stewart <michaelstewartresearch@gmail.com>
To   statalist <statalist@hsphsun2.harvard.edu>
Subject   st: Need help for calculation across observations within variable
Date   Tue, 21 May 2013 09:17:15 -0400

HI,

I am looking to see if anyone could an efficient code than what I have
been using for a particular issues that I am dealing with

My Need

1)Create a variable which shows if the "year" is same or different by pat_name
2)Create a variable which shows number of distinct years ,per patient

My dataset structure is as follows

pt_name     year(string variable)
111             2009
111             2009
111             2009
111             2011
222             2009
222            2009
222            2010

My code is two step one
Step-1: bysort pt_name(year):  gen flag=_n==_N
Step-2:egen max=total(flag),by(pt_name)

Please let me know if there is an more efficient one step code


-- 
Thank you ,
Yours Sincerely,
Mike.
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index