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]

Re: st: stacking unique values of several variables under one new variable


From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: stacking unique values of several variables under one new variable
Date   Mon, 25 Feb 2013 09:35:04 +0100

On Mon, Feb 25, 2013 at 7:15 AM, James Bernard wrote:
> I have a variable ("Technology) that indicates type(s) of a technology
> for each record. I want to aggregate the unique values of this
> variable under one new variable

*---------------- begin example ----------------
// create example data
clear all
input str23 type
"Monoclonals"
"Vaccines"
"Adjuvant, Vaccine"
"Combinchem, Monoclonals"
end

// put each type in a separate variable
split type, p(", ")

// original variable is no longer needed
drop type

// reshape such that the types are stacked
gen id = _n
reshape long type, i(id) j(order)

// id and order are no longer needed
drop id order

// remove missing values
drop if type == ""

// remove duplicates
bys type : keep if _n == 1

// see the results
list
*----------------- end example -----------------
(For more on examples I sent to the Statalist see:
 http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   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