The macro code is below with the macro call below that. The code is lined up and beautiful and easy to read in my program editor but it is hard to read below. If there's a way to make it easier to read for you then please let me know.
The first part of the macro is a proc freq, then a proc logistic that creates a dataset, but you can skip past that and go to the subsequent data step, where is uses that dataset and which is where the problem is.
In my output window, the proc print at the end shows that it's giving the wrong result. In the real macro there is yet more code below that but that's not the point, so I just put %MEND; right after the proc print. And there is no error in my log, although I can post it if you like, but I'm trying not to overwhelm you. My code runs without error but it just gives a result that I don't understand.
The chunk from "TAKE CARE OF X_VAR - START" to "TAKE CARE OF X_VAR - END" is where the problem is so you don't have to look at the stuff after that. The 2nd argument in the macro is x_var. The argument in the macro call that corresponds to that is NatInj_Hand. The if-then-do loop for NatInj_Hand comes first and creates a variable named NatInj_Hand_v and assigns a format to it.
But in the proc print at the end, the variable NatInj_v has a different format assigned to it. It has the format assigned in the if-then-do loop for Sector, which is the last of the three. Or if I comment that format assignment out, then it has the one for MineAct_Hand, which is the second of the three. Or if I comment both of those format assignments out, only then does it have the appropriate format assignment of the on in the NatInj_Hand if-then-do loop.
I hope this isn't too much. If there's anything I can do to make it easier or any questions I can answer then let me know.
%MACRO IMP (bodypart, x_var, x_var_ref, x_levels, group_var, group_var_ref); options mprint; proc freq data=AI_Use; tables &BODYPART.Inj * &X_VAR * &GROUP_VAR / nopct norow; run; ods select ModelInfo NObs ResponseProfile; proc logistic data=AI_Use; title "THIS HAS COVARIABLES &X_VAR AND &GROUP_VAR"; class &X_VAR (ref="&X_VAR_REF") &GROUP_VAR (ref="&GROUP_VAR_REF") / param=ref; model &BODYPART.Inj = &X_VAR &GROUP_VAR &X_VAR * &GROUP_VAR / clodds = wald; oddsratio &X_VAR; ods output OddsRatiosWald = &BODYPART._&X_VAR._&GROUP_VAR; run; proc print data=&BODYPART._&X_VAR._&GROUP_VAR; title "&BODYPART._&X_VAR._&GROUP_VAR"; run; data &BODYPART._&X_VAR._&GROUP_VAR.2; set &BODYPART._&X_VAR._&GROUP_VAR; *********************************************************************************; ************************* TAKE CARE OF X_VAR -- START *************************; *********************************************************************************; %LET X = "&X_VAR"; if &X = "NatInj_Hand" then do; if find (Effect, "&X_VAR_REF") ne 0 then do; if find (Effect, 'Amputation/Enucleation' ) ne 0 then &X_VAR._v = 1; if find (Effect, 'Burns' ) ne 0 then &X_VAR._v = 2; if find (Effect, 'Crushing' ) ne 0 then &X_VAR._v = 3; if find (Effect, 'Cut/Laceration/Puncture') ne 0 then &X_VAR._v = 4; if find (Effect, 'Fracture/Chip' ) ne 0 then &X_VAR._v = 5; if find (Effect, 'Other injury' ) ne 0 then &X_VAR._v = 6; if find (Effect, 'Sprain/Strains' ) ne 0 then &X_VAR._v = 7; * if find (Effect, 'Contusion/Bruise' ) ne 0 then &X_VAR._v = 8; label &X_VAR._v = 'Nature of Injury'; format &X_VAR._v NatInj_Hand.; end; end; if &X = "MineAct_Hand" then do; if find (Effect, "&X_VAR_REF") ne 0 then do; if find (Effect, 'Operating Equipment' ) ne 0 then &X_VAR._v = 1; if find (Effect, 'Handling Material' ) ne 0 then &X_VAR._v = 2; if find (Effect, 'Welding/Grinding' ) ne 0 then &X_VAR._v = 3; if find (Effect, 'Machine Maint./Repair)' ) ne 0 then &X_VAR._v = 4; if find (Effect, 'Hand tools (Not Powered)') ne 0 then &X_VAR._v = 5; if find (Effect, 'Hand tools (Powered)' ) ne 0 then &X_VAR._v = 6; if find (Effect, 'Walking/Running' ) ne 0 then &X_VAR._v = 7; if find (Effect, 'Other' ) ne 0 then &X_VAR._v = 8; * if find (Effect, 'Roofbolting' ) ne 0 then &X_VAR._v = 9; label &X_VAR._v = 'Mine Worker Activity'; format &X_VAR._v MineAct_Hand.; end; end; if &X = "Sector" then do; if find (Effect, "&X_VAR_REF") ne 0 then do; if find (Effect, '1 - Coal' ) ne 0 then &X_VAR._v = 1; if find (Effect, '5 - Sand/Gravel') ne 0 then &X_VAR._v = 2; if find (Effect, '6 - Stone' ) ne 0 then &X_VAR._v = 3; if find (Effect, '8 - Metal' ) ne 0 then &X_VAR._v = 4; * if find (Effect, '7 - Nonmetal' ) ne 0 then &X_VAR._v = 5; label &X_VAR._v = 'Sector'; format &X_VAR._v Sector.; end; end; *********************************************************************************; ************************* TAKE CARE OF X_VAR -- END *************************; *********************************************************************************; *********************************************************************************; *********************** TAKE CARE OF GROUP_VAR -- START ************************; *********************************************************************************; %LET GROUP = "&GROUP_VAR"; if &GROUP = "SubUnit3" then do; if find (Effect, 'A-Underground' ) ne 0 then &GROUP_VAR._v = 1; if find (Effect, 'C-Mills/Prep Plants') ne 0 then &GROUP_VAR._v = 2; if find (Effect, 'B-Surface' ) ne 0 then &GROUP_VAR._v = 3; label &GROUP_VAR._v = 'SubUnit3'; format &GROUP_VAR._v SubUnit.; end; else if &GROUP = "Sector" then do; if find (Effect, "&X_VAR_REF") ne 0 then do; if find (Effect, '1 - Coal' ) ne 0 then &GROUP_VAR._v = 1; if find (Effect, '5 - Sand/Gravel') ne 0 then &GROUP_VAR._v = 2; if find (Effect, '6 - Stone' ) ne 0 then &GROUP_VAR._v = 3; if find (Effect, '8 - Metal' ) ne 0 then &GROUP_VAR._v = 4; if find (Effect, '7 - Nonmetal' ) ne 0 then &GROUP_VAR._v = 5; label &GROUP_VAR._v = 'Sector'; format &GROUP_VAR._v Sector.; end; end; *********************************************************************************; *********************** TAKE CARE OF GROUP_VAR -- END ************************; *********************************************************************************; if &X_VAR._v ne . and &GROUP_VAR._v ne . then output; run; proc print data=&BODYPART._&X_VAR._&GROUP_VAR.2; title "ODDS RATIO OF HAVING A &BODYPART INJURY AS OPPOSED TO A NON-&BODYPART INJURY"; var &X_VAR._v &GROUP_VAR._v OddsRatioEst LowerCL UpperCL; run;
%MEND;
Here is the macro call.
%IMP (Hand, NatInj_Hand , Contusion/Bruise, 7, SubUnit3, B-Surface );
... View more