/* This macro exports data for item analysis in DIGRAM. It outputs 3 files: .dat .var .def All variables must be numeric. The macro is called with the statement %exp_digr(data=work.adl, items=stairs tired walking, exo=age sex, name=h:\data\scd\adlscale) where 'data' is the sas data set with the items and covariates, 'items' is a list of items, 'exo' is a list of covariates and 'name' is the DIGRAM project name. Note that items and covariates are assumed to have at most 8 categories, since DIGRAM uses '9' as the code for missing values. All items and exogenous variables must be scored 0,1, ... */ %macro exp_digr(data, items, exo, name); options nomprint nomlogic nosymbolgen nonotes nostimer; *options mprint mlogic symbolgen notes stimer; /* save number of respondents as macro variable */ data _null_; set &data end=final; if final then call symput('N',trim(left(_N_))); run; /* save the item names as macro variables */ data _null_; set &data; array _y (*) &items; length name $8; if _n_=1 then do; do _i=1 to dim(_y); call vname(_y{_i},name); call symput('_item'||trim(left(put(_i,4.))),trim(left(name))); end; _p=dim(_y); call symput('_nitems',trim(left(put(_p,4.)))); end; run; /* calculate number of exogenous variables - save names as macro variables */ %if %upcase(%left(%trim(&exo)))=NONE %then %do; %let _nexo=0; %end; %else %do; data _null_; set &data; array _y (*) &exo; length name $8; if _n_=1 then do; do _i=1 to dim(_y); call vname(_y{_i},name); call symput('_exo'||trim(left(put(_i,4.))),trim(left(name))); end; _p=dim(_y); call symput('_nexo',trim(left(put(_p,4.)))); end; run; %end; *****************************************************************************; * * Make macro variables for DIGRAM short names * *****************************************************************************; %let _shname = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ; /* determine number of categories for the items */ %do _it = 1 %to &_nitems; proc freq data= &data (where= (&&_item&_it ne .)) noprint; tables &&_item&_it /out= _freq; run; data _null_; set _freq end=last; _number = _N_; if last then call symput("_ncat&_it", trim(left(_number)) ); run; %end; /* determine number of categories for the exogenous variables */ %do _e = 1 %to &_nexo; proc freq data= &data (where= (&&_exo&_e ne .)) noprint; tables &&_exo&_e /out= _freq; run; data _null_; set _freq end=last; _number = _N_; if last then call symput("_ncat%eval(&_nitems+&_e)", trim(left(_number)) ); run; %end; /* output data set */ data _null_; set &data; %do _e = 1 %to &_nitems; if &&_item&_e = . then &&_item&_e = 9; %end; %do _e = 1 %to &_nexo; if &&_exo&_e = . then &&_exo&_e = 9; %end; file "&name..dat"; put &items &exo; run; %put ******************************; %put Note: aschii file output to &name..dat; %put ******************************; /* create the .DEF file*/ data _null_; file "&name..DEF"; put "%eval(&_nitems+&_nexo)"; run; /* create the .VAR file*/ data _null_; file "&name..var"; put "%eval(&_nitems+&_nexo)"; %do _f = 1 %to %eval(&_nitems+&_nexo); %let _short = %substr(&_shname, &_f, 1); put "&_short &_f &&_ncat&_f 3"; %if &&_ncat&_f=2 %then %do; put '0 0 1'; %end; %if &&_ncat&_f=3 %then %do; put '0 0 1 2'; %end; %if &&_ncat&_f=4 %then %do; put '0 0 1 2 3'; %end; %if &&_ncat&_f=5 %then %do; put '0 0 1 2 3 4'; %end; %if &&_ncat&_f=6 %then %do; put '0 0 1 2 3 4 5'; %end; %if &&_ncat&_f=7 %then %do; put '0 0 1 2 3 4 5 6'; %end; %if &&_ncat&_f=8 %then %do; put '0 0 1 2 3 4 5 6 7'; %end; %end; put "2"; put "%eval(&_nitems) %eval(&_nitems+&_nexo)"; put "VARIABLES"; %do _i = 1 %to &_nitems; %let _short = %substr(&_shname, &_i, 1); put "&_short &&_item&_i"; %end; %do _e = 1 %to &_nexo; %let _short = %substr(&_shname, %eval(&_nitems+&_e), 1); put "&_short &&_exo&_e"; %end; output; run; %exit: options notes stimer; %mend exp_digr; libname web 'h:\public_html'; %exp_digr(data= web.ex2, items=Anxiety Anger Hopeless Lonely Restless Dependen Tense Touchy Shame Depress Crying, exo=interv status sex age health, name=h:\ex2)