BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
gx2144
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
gx2144
Calcite | Level 5

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

gx2144_2-1596837447479.png

gx2144_3-1596837460903.png

 

gx2144_4-1596837483042.png

 

REAL OUTPUT

gx2144_4-1596837483042.png

gx2144_4-1596837483042.png

gx2144_4-1596837483042.png

 

 

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
ballardw
Super User

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.

gx2144
Calcite | Level 5
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.

Reeza
Super User
Your program hasn't been designed to take multiple variables, only a single one. You'll need to modify your macro code if you want it to handle multiple values at once. You should first check the code required (non macro) that would handle the multiple variables correctly and see what needs to change.

You cannot just pass a list and expect the macro to operate across the list of variables.
gx2144
Calcite | Level 5
I created three separate macro statement and wanted it to output three different plots for each statement. I did not pass a list actually. But the thing it it output three identical plots which is the output for the third macro statement.
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 753 views
  • 0 likes
  • 4 in conversation