/* this program calculates some of the ego-network statistics (density, etc.) for the full network from one Add Health school. author: moody date: Dec 31, 2000 */ libname in1 's:\fac\jwm\data\'; data a; /*SAS will prompt you for a password to get this program to work */ set in1.s884dat; where aidr>90000000; /* remove people who could not be nominated */ female=s2-1; /* male=0, female=1 */ if s3 > 12 then s3=.; if s3 < 9 then s3=.; /* in this particular school, there are no jr high kids */ grade=s3; run; proc freq; tables female grade; run; proc sort data=a out=a tagsort; by aidr; run; /* program to get the ego-network means. We use these later. Change the directory to read from SPAN folder */ %include 'c:\moody\sas\programs\macros\ego_xmat.mac'; %include 'c:\moody\sas\programs\macros\words.mac'; %include 'c:\moody\sas\programs\macros\numobs.mac'; /* bring in data, recode missings, etc. */ data frnds; /* get friendship partner data */ set a; array frnds mf1aid mf2aid mf3aid mf4aid mf5aid ff1aid ff2aid ff3aid ff4aid ff5aid; do over frnds; if frnds = 77777777 then frnds = .; /* nominations to another school */ if frnds = 88888888 then frnds = .; /* goes to sister school, not in directory */ if frnds = 99999999 then frnds = .; /* not found in the directory */ if frnds = 99959995 then frnds = .; /* bad nomination, miskeyed, etc. */ end; run; proc means data=frnds; run; /* to get the peer data, we need to: (a) construct the friendship network (b) extract the ego-network type we want (s,r,s|r,S&R) (c) Calculate the measure. */ /* construct the friendship network for the school. The origional data consist of a set of attributes and nominations (5 male, 5 female). */ proc iml; %include 'c:\moody\sas\programs\modules\adj.mod'; /* change to match your SPAN directory */ %include 'c:\moody\sas\programs\modules\pajwrite.mod'; /* ditto */ %include 'c:\moody\sas\programs\modules\pajpart.mod'; /* ditto */ use work.frnds; read all var{aidr} into aid; read all var{mf1aid mf2aid mf3aid mf4aid mf5aid} into mnoms; /* male friends */ read all var{ff1aid ff2aid ff3aid ff4aid ff5aid} into fnoms; /* female friends */ noms=mnoms||fnoms; /* you could, of course, skip this line and modify below for only male or female networks */ read all var{grade} into grade; read all var{female} into sex; grade=choose(grade=.,0,grade); /* change missing to zero: Only for PAJEK plot */ sex=choose(sex=.,9,sex); /* change missing sex to a 9: Only for PAJEK plot */ adjmat=adj(aid,noms); /* create the adjacency matrix, using the SPAN module */ adjid=adjmat[,1]; /* pull off the id variable, which is in the first column */ adjmat=adjmat[,2:ncol(adjmat)]; /* now have a square n by n adj. matrix */ /* need to pull out people who are not also sampled. These are kids who were nominated, but not sampled in the survey. The next lines creates a vector, called sampled, that tells us who is in the sample. */ sampled=j(nrow(adjmat),1,0); do i=1 to nrow(adjid); /* look over every person in the network */ iloc=loc(aid=adjid[i]); /* see if you can find them in the AID matrix */ if type(iloc)='N' then do; sampled[i]=1; /* if so, they were sampled, change value to 1 */ free iloc; end; end; keep=loc(sampled=1); /* reduce the network to people sampled in the in-home data */ adjid=adjid[keep,1]; adjmat=adjmat[keep,keep]; reset storage=work.tempnet; store adjmat adjid; /* store the network, for now, we reload it in the macro below */ file "c:\moody\temp\schl27.net"; /* write it out to PAJEK just to look at it */ call pajwrite(adjmat,adjid,2); file "c:\moody\temp\schl27_grade.clu"; /* write out grade partition too */ call pajpart(grade); file "c:\moody\temp\schl27_sex.clu"; /* write out grade partition too */ call pajpart(sex); quit; /* calculate the ego-network mean data, here the proportion female and the mean grade */ %ego_xmat(scode, aidr, adjmat, Grade female, /* this is the list of variables I want the means for */ "SOR", /* get the 'sent' or 'Recieved' network */ work.tempnet, work.frnds, work.s_emean); proc means data=work.s_emean; run; /* now we want to calculate some measures on the _structure_ of the ego-network, using the full network data as our input. Note, that you can calculate many of these using UCINET V directly, by exporting the data to UCINET, then going to EGO-NETWORK on the window. This is not implemented in UCINET IV */ proc iml; %include 'c:\moody\sas\programs\modules\density.mod'; /* program that calculates density */ reset storage=work.tempnet; load adjmat adjid; /*to calculate ego-network density, we need to pull out a small adjacency matrix of only the people that ego sends or recieves ties from. Below I spell out the code, so you can see how it works. But you could also use egonet.mod to get the same thing. */ egoden=j(nrow(adjid),1,.); /* make an empty matrix to store results in */ do i=1 to nrow(adjid); /* do for each person in the network */ sendi=loc(adjmat[i,]^=0); /* cols that ego sends to */ recvi=loc(adjmat[,i]^=0); /* rows that nominate ego */ if type(sendi)='N' & type(recvi)='N' then do; snr_i=union(sendi,recvi); /* by taking union, we get everyone ego nominates or who nominates ego. */ end; else if type(sendi)='N' then do; snr_i=sendi; end; else if type(recvi)='N' then do; snr_i=recvi; end; if ncol(snr_i)>1 then do; /* if they are connected to more than one person */ submati=adjmat[snr_i,snr_i]; deni=density(submati); egoden[i]=deni; end; end; outv=adjid||egoden; /* make one matrix with both id and egoden #s */ create ed_dat from outv [colname={aidr egoden}]; append from outv; quit; proc means data=ed_dat; run; /* now we merge the ego-density data with the ego-mean data and the basic demographic data to answer the homework question */ data egodat; merge ed_dat s_emean frnds; by aidr; run; /* modify the following to get your answer */ proc means; var egoden emfemale emgrade; run;