Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: combining two variables


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: combining two variables
Date   Thu, 02 Feb 2006 19:39:42 -0500

If all of the vars that begin a_ have a matching b_ var, and all are numeric, then you could try something like

foreach var in varlist a_* {
local stub=substr("`var'",3,.)
gen c_`stub'=cond(b_`stub'==.,`var',b_`stub')
}

If the data come from different datasets, you might want to look at -merge- with the update option.

Michael Blasnik

p.s. For proper Statalist etiquette, avoid saying things like "i have been trying to figure out a way to do this with the foreach command. but it doesn't seem to be working out." Instead, you should show us what you typed and why "it didn't work out."


----- Original Message ----- From: "Daphna Bassok" <[email protected]>
To: <[email protected]>
Sent: Thursday, February 02, 2006 7:13 PM
Subject: st: combining two variables



hi all-

i have a data structure question, and i could use some advice.

suppose i have two variables say a_school and b_school. the values of these two variables are either the same, or one of the two might be missing. i would like to somehow merge these into a single variable- say c_school.

if i only had to do this once, it would be quite simple.

gen c_school=b_school
replace c_school=a_school if c_school==.

done.

however, i have to do this repeatedly. as in... i have many of these pairs. a_size, b_size... a_score, b_score...etc.



does anyone have any suggestions?

any help would be appreciated.

daphna
*
*   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