I'm starting to get a little more comfortable with macro programming myself, but I'm still learning. The main thing to understand is that the macro facility really only regenerates text. It isn't actually running any data steps or processing anything in SAS. Once I understood that, they started to make more sense to me. Once it generates the text, then SAS data steps process that. Here's something you could play around with. There are other folks on here who are better who can probably clean up my code. Also, one thing I noted was that your 2014 & 2015 datasets were called hiv2015 & hiv2015, but then you appended the base with lgbt2015 and lgbt2016. The macro below would just append everything with datasets starting hiv. You also have a proc sort on lgbt2014, but all your procs are referencing hiv2014 (or hiv_all). %include filepath with the two embedded macros to compile (add in the proc format?)
%macro main(max);
%do i=2014 %to &max.;
libname b&i xport "/folders/myfolders/HIV-Millennials/LLCP&i..XPT";
libname hiv_data '/folders/myfolders/HIV-Millennials';
data hiv_data.b&i;
length _STATE 8 ADDEPEV2 8 EDUCA 8 HIVTST6 8 MARITAL 8 SEX 8 SXORIENT 8 HLTHPLN1 8 TRNSGNDR 8 ;
set b&i.llcp&i;
run;
proc format;
value educa 0='No College' 1='College or Degree';
value agecat 1='Millennials' 2='Non-millennials';
value sex 0='Male' 1='Female';
value health 0='No' 1='Yes';
value trnsgndr 1='Yes' 0='No';
value hiv 0='No' 1='Yes';
value sxorient 1='Straight' 2='Lesbian/Gay' 3='Bisexual' 4='Other';
value race 0='White' 1='Black' 2='Hispanic' 3='Other';
value state 0='West' 1='Southwest' 2='Midwest' 3='Southeast' 4='Northeast'
5='U.S. Territory';
value married 0='Not Married' 1='Married';
value depression 0='No previous depression' 1='Previous depression';
run;
%if &i = 1 %then %do;
data hiv_final (keep=_race _age_g ADDEPEV2 _state trnsgndr marital hivtst6 sex agecat hlthpln1
educa sxorient);
set hiv_data.b&i;
%filters;
%conditions;
run;
%end;
%else %do;
data hiv&i (keep=_race _age_g ADDEPEV2 _state trnsgndr marital hivtst6 sex agecat hlthpln1
educa sxorient);
set hiv_data.b&i;
%filters;
%conditions;
run;
proc append base=hiv_final data=hiv&i;
run; %end;
* sort the appended data by sex ;
proc sort data=hiv_final;
by sex;
run;
* run frequency/association testing on covariates against variable of interest ;
proc freq data=hiv_final;
tables (ADDEPEV2 sex _race marital _state trnsgndr sxorient hlthpln1 educa) *
agecat / chisq;
format sex sex. agecat agecat. _race race. marital married.
_state state. sxorient sxorient. hlthpln1 health.
educa educa. trnsgndr trnsgndr. ADDEPEV2 depression. ;
run;
/* univariate analysis of covariates and exposure against outcome */
proc freq data=hiv_final;
tables (ADDEPEV2 sex agecat _race marital _state trnsgndr sxorient hlthpln1
educa) * hivtst6 / chisq;
format sex sex. agecat agecat. _race race. marital married.
_state state. sxorient sxorient. hlthpln1 health.
educa educa. trnsgndr trnsgndr. hivtst6 hiv. ADDEPEV2 depression.;
run;
/* logistic regression of covariates against ever been told had depressive diagnosis */
proc logistic data=hiv_final;
class hivtst6(ref='1') ADDEPEV2(ref='1') sex(ref='0') agecat(ref='2')
_race(ref='0') marital(ref='0') _state(ref='2') trnsgndr(ref='0')
sxorient(ref='1') hlthpln1(ref='0') educa(ref='0');
model hivtst6=ADDEPEV2 sex agecat _race marital _state trnsgndr sxorient
hlthpln1 educa;
run;
%mend main();
%main(2018) /*or maybe pass in the function year(today())*/
... View more