/* this macro converts SAS network data files into files that can be used by R, and thus STATNET. You must create an edge-level dataset with the information you want (all edge-level covariates, say). You can also specify a node-level file with node attributes and a dyad-level file with dyad-covariates. The dyad file and the edge file must have matching node ids for the 'ends' of the edges. The IDs should run from 1 to N, inclusive. The routine writes out all variables in the input datasets to SAS XPORT files. This means that variable names have to be 8 chars or less (we could change and write out an excell or CSV sheet ...). The macro arguments are: %send2r(libdir = , *physical directory where the xport files will be put; xptedat=, *SAS export dataset name for edge file; xptdydat=, *SAS export dataset name for all-dyadcovar file; xptvdat=, *SAS export dataset name for node-level covar file; redatnm=, *Name for R data object w. edge information rdydatnm=, *Name for R data object w. all-dyadcovar info; rvdatnm=, *Name for R data object w. all node covar info; rscript=, *name of the R script file you will run. Will be placed in libdir; sasedat=, *name of the SAS edge-level file, that includes edge-present covars sasvdat=, *name of the SAS node-level file sasdydat=, *name of the SAS all-dyad file for dyad-level covar node1=, *ID name for one end of edges (sender) node2= ); *ID name for other end of eges (recver) Required terms are: libdir, xptedat, redatnm, sasedat, node1, node2. NOTE: the libdir directory must be written with FORWARD SLASHES -- even on Windows systesm. THis is what R needs (and SAS is fine with that). Author: Moody Date: March, 2007 */ %macro send2r(libdir=, xptedat=, xptdydat=, xptvdat=, redatnm=, rdydatnm=, rvdatnm=, rscript=, rnetnm=, rnetcovnm=, sasedat=, sasvdat=, sasdydat=, Node1=, node2=); /* write out SAS file w. edgefile data */ libname out1 xport "&libdir.&xptedat..xpt"; data out1.&xptedat; set &sasedat; run; %if %length(&sasvdat)>0 %then %do; /* there is a vertex attrib set */ libname out2 xport "&libdir.&xptvdat..xpt"; data out2.&xptvdat; set &sasvdat; run; %end; %let nn1 = %upcase(&node1); %let nn2 = %upcase(&node2); /* construct the R program for reading the data */ data null; nn1=quote(compress(&nn1)); nn2=quote(compress(&nn2)); run; data null; set null; file "&libdir.&rscript"; put "#Program to create network data in R from SAS "; put "# load R packages "; put "library(foreign)"; put "library(statnet)"; put "library(network)"; put "#Read in the edge-level data from external file"; put "&redatnm <- read.xport('&libdir.&xptedat..xpt')"; put "#create a network object from the Node names of edgelist"; put "&rnetnm <- network(&redatnm.[,c(" nn1 "," nn2 ")])"; /* assumes network id in first 2 columns */ put "summary(&rnetnm)"; %if %length(&sasvdat)>0 %then %do; /* there are additional vertex attribs */ put "#Now create the node attributes "; put "&rvdatnm <- read.xport('&libdir.&xptvdat..xpt')"; %end; run; /* want to loop over all vars in the dataset and write them out as vertex attributes in the network object. */ %if %length(&sasvdat)>0 %then %do; /* there are additional vertex attribs */ proc contents data=&sasvdat noprint out=_vcont; run; data _vcont; set _vcont; file "&libdir.&rscript" mod; nameq=quote(compress(name)); put "&rnetnm" ' %v% ' nameq "<- &rvdatnm[," varnum "]"; /* read from dataset */ run; data null; file "&libdir.&rscript" mod; put "list.vertex.attributes(&rnetnm)"; run; %end; data null; file "&libdir.&rscript" mod; put "#Now assign edge attributes "; run; proc contents data=&sasedat noprint out=_econt; run; data _econt; set _econt; file "&libdir.&rscript" mod; nameq=quote(compress(name)); * put "&rnetnm" ' %e% ' nameq "<- &redatnm[," varnum "]"; put "set.edge.attribute(&rnetnm," nameq ",&redatnm.[," varnum "])"; run; data null; file "&libdir.&rscript" mod; put "list.edge.attributes(&rnetnm)"; run; %if %length(&sasdydat)>0 %then %do; /* there are dyad-level covars */ libname out3 xport "&libdir.&xptdydat..xpt"; data out3.&xptdydat; set &sasdydat; run; data null; nn1=quote(compress(&nn1)); nn2=quote(compress(&nn2)); file "&libdir.&rscript" mod; put "#Now assign dyad values "; put "&rdydatnm <- read.xport('&libdir.&xptdydat..xpt')"; put "&rnetcovnm <- network(&rdydatnm.[,c(" nn1 "," nn2 ")])"; /* assumes same node id var name */ put "summary(&rnetcovnm)"; run; proc contents data=&sasdydat noprint out=_dycont; run; data _dycont; set _dycont; file "&libdir.&rscript" mod; nameq=quote(compress(name)); *put "&rnetcovnm" ' %e% ' nameq "<- &rdydatnm[," varnum "]"; put "set.edge.attribute(&rnetcovnm," nameq ",&rdydatnm.[," varnum "])"; run; %end; %mend;