Dyadkey

February 26, 2010 at 4:47 am

| Gabriel |

I wrote this little program to create a symmetric id for a dyad so that it will have the same value regardless of who is ego and who is alter. I wrote it because one of my grad students was having trouble with a dataset on mergers in that it was hard to get the computer to understand that “exxon_mobil” is the same thing as “mobil_exxon”. The solution is to just get it so that the two components always combine in alphabetical order (or any other order, it doesn’t matter, as long as it’s consistent). If you’re using numeric key variables you might want to “string()” them before running it.

capture program drop dyadkey
program define dyadkey
	local a_var `1'
	local b_var `2'
	if "`3'"=="" {
		local dyadkey "dyadkey"
	}
	else {
		local dyadkey `3'
	}
	gen `dyadkey'=""
	local lastrow=[_N]
	forvalues row=1/`lastrow' {
		local aval=`a_var' in `row'
		local bval=`b_var' in `row'
		local ab="`aval'"+"_"+"`bval'"
		local ba="`bval'"+"_"+"`aval'"
		if "`ab'">"`ba'" {
			quietly replace `dyadkey'="`ab'" in `row'
		}
		if "`ba'">="`ab'" {
			quietly replace `dyadkey'="`ba'" in `row'
		}
	}
end

It takes as arguments the ego id, the alter id, and (optional) the desired name for the new dyad id. For example:
dyadkey part1_cusip part2_cusip joint_cusip

Entry filed under: Uncategorized. Tags: , .

Soc of Mass Media, week 8 More R headaches


The Culture Geeks


%d bloggers like this: