Statalist


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

Re: st: Creating id number by subgroup


From   "Scott Merryman" <[email protected]>
To   [email protected]
Subject   Re: st: Creating id number by subgroup
Date   Tue, 6 Jan 2009 12:17:53 -0600

You could combine a -foreach- loop with egen:

clear
input id  str1 title
1    "A"
1    "A"
1    "B"
1    "B"
1    "B"
2    "C"
2    "C"
2    "C"
2    "D"
2    "D"
3    "E"
end

gen var = .
levelsof id, local(levels)
foreach l of local levels {
	egen var2 = group(title) if id == `l'
	replace var = var2 if id == `l'
                drop var2
}
l


On Tue, Jan 6, 2009 at 12:05 PM, Mike Kim <[email protected]> wrote:
> Dear statalisters,
>
> I would like to create 'var1' in the following table based on id and title.
> I tried "bysort id title: gen var2=_n" and this creates var2 which is
> different from var1.
> I also tried "by id: egen var1=group(title)", but egen does not allow 'by'.
> How can I create var1?
>
> id  title  var1  var2
> 1    A      1    1
> 1    A      1    2
> 1    B      2    1
> 1    B      2    2
> 1    B      2    3
> 2    C      1    1
> 2    C      1    2
> 2    C      1    3
> 2    D      2    1
> 2    D      2    2
> 3    E      1    1
> .......
>
> Thank you in advance.
> Mike.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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