Hi, I have a problem outputting a macro.
%macro bysex(var,n, name);
proc univariate data=work1;
class y1p_G_GIRL;
var &var;
histogram &var / nrows=2 outhist=OutHist&n
odstitle="&name by Gender";
ods select histogram;
run;
data work.butterflysex&name;
keep &var Males Females;
label Males = Females = y1pi_VT = ;
merge OutHist&n(where = (y1p_G_GIRL = 1) rename = (_COUNT_ = Females _MIDPT_ = &var))
OutHist&n(where = (y1p_G_GIRL = 0) rename = (_COUNT_ = Males _MIDPT_ = &var));
by &var;
Males = -Males;
run;
ods graphics / reset;
title "Butterfly Plot of &name Counts By Gender";
proc sgplot data=work.butterflysex&name;
format Males Females positive.;
hbar &var / response=Males legendlabel="Males";
hbar &var / response=Females legendlabel="Females";
xaxis label="Count" grid
min=-150 max=150 values=(-150 to 150 by 10) valueshint;
yaxis label="&name" discreteorder=data;
run;
%mend bysex;
%bysex(y1pi_VT, 1,VT)
%bysex(y1pi_R5Hz, 2,R5Hz)
%bysex(y1pi_R10Hz, 3,R10Hz)
The results output three plots of %bysex(y1pi_R10Hz, 3,R10Hz), but I want to results to output these three variables separately. Can somebody debug my code?Appreciated!
The problem is
ods graphics / reset;
Each time this runs, it sets the output file to have index = 1, or in other words, it overwrites the previous plot, and so on the third run, there still is only one plot which contains the third output.
I'd be happy to help, but you could explain more about what the code is supposed to be doing, how the code does this, show us the output you get and describe or show us the output you want.
My problem is I need to output the results for these three macros,which should be different.
(%bysex(y1pi_VT, 1,VT)
%bysex(y1pi_R5Hz, 2,R5Hz)
%bysex(y1pi_R10Hz, 3,R10Hz)
When I run these three sentences separately, it works fine.But when I run these together, it output three plots of R10Hz.
EXPECTED RESULTS
REAL OUTPUT
First, please follow the advice from @ballardw and if that doesn't help, show us the log of the code with MPRINT turned on. To show us the log, please preserve the formatting of the log by copying the log as text and pasting it into the box that appears when you click on the </> icon.
I suggest that you set OPTIONS MPRINT, rerun the 3 macro calls to generate the output and then read the log carefully. If you don't see a problem then show us the log. Copy the entire log from running the three calls with all the MPRINT output, notes, warnings and/or errors. Paste all of that into a code box opened on the forum with the </> icon. This is important to preserve formatting of text.
1676 %macro bysex(var,n, name); 1677 proc univariate data=work1; 1678 class y1p_G_GIRL; 1679 var &var; 1680 histogram &var / nrows=2 outhist=OutHist&n 1681 odstitle="&name by Gender"; 1682 ods select histogram; 1683 run; 1684 1685 data work.butterflysex&name; 1686 keep &var Males Females; 1687 label Males = Females = y1pi_VT = ; 1688 merge OutHist&n(where = (y1p_G_GIRL = 1) rename = (_COUNT_ = Females _MIDPT_ = &var)) 1689 OutHist&n(where = (y1p_G_GIRL = 0) rename = (_COUNT_ = Males _MIDPT_ = &var)); 1690 by &var; 1691 Males = -Males; 1692 run; 1693 1694 ods graphics / reset; 1695 title "Butterfly Plot of &name Counts By Gender"; 1696 proc sgplot data=work.butterflysex&name; 1697 format Males Females positive.; 1698 hbar &var / response=Males legendlabel="Males"; 1699 hbar &var / response=Females legendlabel="Females"; 1700 xaxis label="Count" grid 1701 min=-150 max=150 values=(-150 to 150 by 10) valueshint; 1702 yaxis label="&name" discreteorder=data; 1703 run; 1704 1705 %mend bysex; 1706 OPTIONS MPRINT; 1707 %bysex(y1pi_VT, 1,VT) MPRINT(BYSEX): proc univariate data=work1; MPRINT(BYSEX): class y1p_G_GIRL; MPRINT(BYSEX): var y1pi_VT; MPRINT(BYSEX): histogram y1pi_VT / nrows=2 outhist=OutHist1 odstitle="VT by Gender"; MPRINT(BYSEX): ods select histogram; MPRINT(BYSEX): run; NOTE: The data set WORK.OUTHIST1 has 17 observations and 5 variables. NOTE: PROCEDURE UNIVARIATE used (Total process time): real time 1.16 seconds cpu time 0.32 seconds MPRINT(BYSEX): data work.butterflysexVT; MPRINT(BYSEX): keep y1pi_VT Males Females; MPRINT(BYSEX): label Males = Females = y1pi_VT = ; MPRINT(BYSEX): merge OutHist1(where = (y1p_G_GIRL = 1) rename = (_COUNT_ = Females _MIDPT_ = y1pi_VT)) OutHist1(where = (y1p_G_GIRL = 0) rename = (_COUNT_ = Males _MIDPT_ = y1pi_VT)); MPRINT(BYSEX): by y1pi_VT; MPRINT(BYSEX): Males = -Males; MPRINT(BYSEX): run; NOTE: There were 7 observations read from the data set WORK.OUTHIST1. WHERE y1p_G_GIRL=1; NOTE: There were 10 observations read from the data set WORK.OUTHIST1. WHERE y1p_G_GIRL=0; NOTE: The data set WORK.BUTTERFLYSEXVT has 10 observations and 3 variables. NOTE: DATA statement used (Total process time): real time 0.02 seconds cpu time 0.01 seconds MPRINT(BYSEX): ods graphics / reset; MPRINT(BYSEX): title "Butterfly Plot of VT Counts By Gender"; MPRINT(BYSEX): proc sgplot data=work.butterflysexVT; MPRINT(BYSEX): format Males Females positive.; MPRINT(BYSEX): hbar y1pi_VT / response=Males legendlabel="Males"; MPRINT(BYSEX): hbar y1pi_VT / response=Females legendlabel="Females"; MPRINT(BYSEX): xaxis label="Count" grid min=-150 max=150 values=(-150 to 150 by 10) valueshint; MPRINT(BYSEX): yaxis label="VT" discreteorder=data; MPRINT(BYSEX): run; NOTE: Since no format is assigned, the numeric category variable will use the default of BEST6. NOTE: PROCEDURE SGPLOT used (Total process time): real time 0.47 seconds cpu time 0.09 seconds NOTE: Some of the tick values have been thinned. NOTE: There were 10 observations read from the data set WORK.BUTTERFLYSEXVT. 1708 %bysex(y1pi_R5Hz, 2,R5Hz) MPRINT(BYSEX): proc univariate data=work1; MPRINT(BYSEX): class y1p_G_GIRL; MPRINT(BYSEX): var y1pi_R5Hz; MPRINT(BYSEX): histogram y1pi_R5Hz / nrows=2 outhist=OutHist2 odstitle="R5Hz by Gender"; MPRINT(BYSEX): ods select histogram; MPRINT(BYSEX): run; NOTE: The data set WORK.OUTHIST2 has 22 observations and 5 variables. NOTE: PROCEDURE UNIVARIATE used (Total process time): real time 0.55 seconds cpu time 0.31 seconds MPRINT(BYSEX): data work.butterflysexR5Hz; MPRINT(BYSEX): keep y1pi_R5Hz Males Females; MPRINT(BYSEX): label Males = Females = y1pi_VT = ; MPRINT(BYSEX): merge OutHist2(where = (y1p_G_GIRL = 1) rename = (_COUNT_ = Females _MIDPT_ = y1pi_R5Hz)) OutHist2(where = (y1p_G_GIRL = 0) rename = (_COUNT_ = Males _MIDPT_ = y1pi_R5Hz)); MPRINT(BYSEX): by y1pi_R5Hz; MPRINT(BYSEX): Males = -Males; MPRINT(BYSEX): run; NOTE: Variable y1pi_VT is uninitialized. NOTE: There were 11 observations read from the data set WORK.OUTHIST2. WHERE y1p_G_GIRL=1; NOTE: There were 11 observations read from the data set WORK.OUTHIST2. WHERE y1p_G_GIRL=0; NOTE: The data set WORK.BUTTERFLYSEXR5HZ has 11 observations and 3 variables. NOTE: DATA statement used (Total process time): real time 0.02 seconds cpu time 0.03 seconds MPRINT(BYSEX): ods graphics / reset; MPRINT(BYSEX): title "Butterfly Plot of R5Hz Counts By Gender"; MPRINT(BYSEX): proc sgplot data=work.butterflysexR5Hz; MPRINT(BYSEX): format Males Females positive.; MPRINT(BYSEX): hbar y1pi_R5Hz / response=Males legendlabel="Males"; MPRINT(BYSEX): hbar y1pi_R5Hz / response=Females legendlabel="Females"; MPRINT(BYSEX): xaxis label="Count" grid min=-150 max=150 values=(-150 to 150 by 10) valueshint; MPRINT(BYSEX): yaxis label="R5Hz" discreteorder=data; MPRINT(BYSEX): run; NOTE: Since no format is assigned, the numeric category variable will use the default of BEST6. NOTE: PROCEDURE SGPLOT used (Total process time): real time 0.52 seconds cpu time 0.06 seconds NOTE: Some of the tick values have been thinned. NOTE: There were 11 observations read from the data set WORK.BUTTERFLYSEXR5HZ. 1709 %bysex(y1pi_R10Hz, 3,R10Hz) MPRINT(BYSEX): proc univariate data=work1; MPRINT(BYSEX): class y1p_G_GIRL; MPRINT(BYSEX): var y1pi_R10Hz; MPRINT(BYSEX): histogram y1pi_R10Hz / nrows=2 outhist=OutHist3 odstitle="R10Hz by Gender"; MPRINT(BYSEX): ods select histogram; MPRINT(BYSEX): run; NOTE: The data set WORK.OUTHIST3 has 21 observations and 5 variables. NOTE: PROCEDURE UNIVARIATE used (Total process time): real time 0.77 seconds cpu time 0.32 seconds MPRINT(BYSEX): data work.butterflysexR10Hz; MPRINT(BYSEX): keep y1pi_R10Hz Males Females; MPRINT(BYSEX): label Males = Females = y1pi_VT = ; MPRINT(BYSEX): merge OutHist3(where = (y1p_G_GIRL = 1) rename = (_COUNT_ = Females _MIDPT_ = y1pi_R10Hz)) OutHist3(where = (y1p_G_GIRL = 0) rename = (_COUNT_ = Males _MIDPT_ = y1pi_R10Hz)); MPRINT(BYSEX): by y1pi_R10Hz; MPRINT(BYSEX): Males = -Males; MPRINT(BYSEX): run; NOTE: Variable y1pi_VT is uninitialized. NOTE: Missing values were generated as a result of performing an operation on missing values. Each place is given by: (Number of times) at (Line):(Column). 1 at 2:208 NOTE: There were 11 observations read from the data set WORK.OUTHIST3. WHERE y1p_G_GIRL=1; NOTE: There were 10 observations read from the data set WORK.OUTHIST3. WHERE y1p_G_GIRL=0; NOTE: The data set WORK.BUTTERFLYSEXR10HZ has 11 observations and 3 variables. NOTE: DATA statement used (Total process time): real time 0.02 seconds cpu time 0.01 seconds MPRINT(BYSEX): ods graphics / reset; MPRINT(BYSEX): title "Butterfly Plot of R10Hz Counts By Gender"; MPRINT(BYSEX): proc sgplot data=work.butterflysexR10Hz; MPRINT(BYSEX): format Males Females positive.; MPRINT(BYSEX): hbar y1pi_R10Hz / response=Males legendlabel="Males"; MPRINT(BYSEX): hbar y1pi_R10Hz / response=Females legendlabel="Females"; MPRINT(BYSEX): xaxis label="Count" grid min=-150 max=150 values=(-150 to 150 by 10) valueshint; MPRINT(BYSEX): yaxis label="R10Hz" discreteorder=data; MPRINT(BYSEX): run; NOTE: Since no format is assigned, the numeric category variable will use the default of BEST6. NOTE: PROCEDURE SGPLOT used (Total process time): real time 0.53 seconds cpu time 0.11 seconds NOTE: Some of the tick values have been thinned. NOTE: There were 11 observations read from the data set WORK.BUTTERFLYSEXR10HZ.
Here is the log output for my macro. I am not an expertise in sas and hope you could help me out.
The problem is
ods graphics / reset;
Each time this runs, it sets the output file to have index = 1, or in other words, it overwrites the previous plot, and so on the third run, there still is only one plot which contains the third output.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: