Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: using SAS-like arrays to create new variables


From   Rafal Raciborski <[email protected]>
To   [email protected]
Subject   Re: st: using SAS-like arrays to create new variables
Date   Sat, 24 Sep 2005 13:22:30 -0400

Kathy,

There are more elegant solutions but the ad hoc code below will work for you. Just change child1-child3 to child1-child7.

rafal


. list

+--------------------------+
| child1 child2 child3 |
|--------------------------|
1. | 1 2 3 |
2. | 2 1 3 |
3. | 3 1 1 |
4. | 2 3 3 |
5. | 1 1 1 |
+--------------------------+

gen sum1=0
gen sum2=0
gen sum3=0

foreach var of varlist child1-child3 {
replace sum1=sum1+1 if `var'==1
replace sum2=sum2+1 if `var'==2
replace sum3=sum3+1 if `var'==3
}

. list

+-----------------------------------------------+
| child1 child2 child3 sum1 sum2 sum3 |
|-----------------------------------------------|
1. | 1 2 3 1 1 1 |
2. | 2 1 3 1 1 1 |
3. | 3 1 1 2 0 1 |
4. | 2 3 3 0 1 2 |
5. | 1 1 1 3 0 0 |
+-----------------------------------------------+


================
Rafal Raciborski
Graduate student
Department of Political Science
Emory University
301 Tarbutton Hall
1555 Dickey Drive
Atlanta, GA 30322
404-378-9826 (home)
[email protected]
http://www.roofoos.net/


*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* 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