Data visualization with SAS programming

Proc SGplot - formatting X axis

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Proc SGplot - formatting X axis

[ Edited ]

Hi,

 

Please see the attached.

 

Obvious question - I need the values on X axis to be spread the entire axis equidistant. 

Here is my code 

 

 

%let colour = %str(red|green|blue);

proc sgplot data=final noautolegend;
	vbox aval / category=avisitn datalabel=usubjid boxwidth=1;

	%do i = 1 %to &n.;
		%let col&i = %scan(&colour,&i,'|');
		refline &&minref&i &&maxref&i / axis=y lineattrs=(pattern=shortdash color=&&col&i);
	%end;

	yaxis type=linear values=(&minval. to &maxval. by &byvar.) label=%sysfunc(compbl("&param."));
	xaxis type=linear values=(1 2 3 4 5 6 7) label="Visit";
run;

 

It seems there is something obvious that I'm missing here. Any help would be appreciated.

 

Thanks,

D

 

delete_boxplot.jpg


Accepted Solutions
Solution
‎08-29-2017 03:23 PM
SAS Super FREQ
Posts: 1,139

Re: Proc SGplot - formatting X axis

[ Edited ]

Somethings are not matching between the graph and the code you have.provided.  You have provided x-axis label of "Visit", but I don't see it on the axis.  Also, I assume the AVISITN values are from 1-7, but it it is not cleary what is causing the large offsets on left or right.  I suggest try setting xaxis offsetmin=0 offsetmax=0 to see if the axis stretches the full width.  If yes, you could put in the right fractional value to get what you want.  But, still it is not clear what is causing the offsets.

 

It would be easier to help if you send a program with built in (sample) data as part of the program.  Then we can try running your program.  Which SAS release do you have?

 

I ran this code and it worked for me.  I removed DataLabel=usubjid and the macro lines for reference lines.  Note I did get the "Visit" label on the x-axis.

 

data final;
  do avisitn=2, 6, 7;
    do j=1 to 100;
	  aval=100+80*ranuni(2);
	  output;
	end;
  end;
run;

proc sgplot data=final noautolegend;
vbox aval / category=avisitn  boxwidth=1  ;
xaxis type=linear values=(1 2 3 4 5 6 7) label="Visit" ;
run;

Box1.png

View solution in original post


All Replies
Trusted Advisor
Posts: 1,784

Re: Proc SGplot - formatting X axis

Downloading of Microsoft Office documents is discouraged, as it is a security risk. So, I can't see the problem.

Contributor
Posts: 24

Re: Proc SGplot - formatting X axis

Can't paste the plot here. Don't know how to present the plot.

 

Main problem is - the values on x axis are concentrated near the centre, taking 1/3 of the total space on x axis. So the rest 2/3 are just empty. I want the values to be spread equally on the entire x axis

Trusted Advisor
Posts: 1,784

Re: Proc SGplot - formatting X axis

Attach plot as pdf file?

Solution
‎08-29-2017 03:23 PM
SAS Super FREQ
Posts: 1,139

Re: Proc SGplot - formatting X axis

[ Edited ]

Somethings are not matching between the graph and the code you have.provided.  You have provided x-axis label of "Visit", but I don't see it on the axis.  Also, I assume the AVISITN values are from 1-7, but it it is not cleary what is causing the large offsets on left or right.  I suggest try setting xaxis offsetmin=0 offsetmax=0 to see if the axis stretches the full width.  If yes, you could put in the right fractional value to get what you want.  But, still it is not clear what is causing the offsets.

 

It would be easier to help if you send a program with built in (sample) data as part of the program.  Then we can try running your program.  Which SAS release do you have?

 

I ran this code and it worked for me.  I removed DataLabel=usubjid and the macro lines for reference lines.  Note I did get the "Visit" label on the x-axis.

 

data final;
  do avisitn=2, 6, 7;
    do j=1 to 100;
	  aval=100+80*ranuni(2);
	  output;
	end;
  end;
run;

proc sgplot data=final noautolegend;
vbox aval / category=avisitn  boxwidth=1  ;
xaxis type=linear values=(1 2 3 4 5 6 7) label="Visit" ;
run;

Box1.png

Contributor
Posts: 24

Re: Proc SGplot - formatting X axis

Hi Sanjay,

 

The label for x-axis and y-axis are not shown on the attached file as I copied the plot from a pdf output onto a MS word doc.

The AVISITN is from 1-7, yes. 

Without the macro lines for refline  and datalabel=usubjid, I'm getting the same plot as you have got.

 

My guess is the probably the refline statement is causing the problem but can't figure out why?

 

I'll try the offsetmin and offsetmax as you've suggested. If that doesn't work, I'll put a sample data with my code.

SAS Super FREQ
Posts: 1,139

Re: Proc SGplot - formatting X axis

[ Edited ]

We can better help debug this if you include the full code, with all the macro variables and sample data so we can run the program to see the results.  If you have long usubjid data labels, they will contribute of offsets.  But I don't see any data labels either.

Contributor
Posts: 24

Re: Proc SGplot - formatting X axis

Hi Sanjay,

 

The offsetmin/max = 0.1 worked. Thanks for that.

 

But I'm still unsure why the problem occured at the first place. So I ran my code without those options. Here is my code and output and attached is the data (sample) in pdf.

 

%macro labplot(paramcd = , minval = , maxval= , byvar= );

options mprint mlogic;

data adlb;
   set adam.adlb;
   where saffl = "Y" and aval ne . and paramcd = "%qupcase(&paramcd)";
   param = propcase(param);
   keep usubjid avisitn avisit paramcd param trtan aval anrlo anrhi;
run;

proc sort data = adlb out=final;
  by usubjid;
run;

proc sql noprint;
      select param
        into: param
        from final;

      select count(distinct anrlo)
	  into: n
         from final;

      select distinct anrlo
          into: minref1 - : minref%cmpres(&n.)
	  from final;

      select distinct anrhi
          into: maxref1 - : maxref%cmpres(&n.)
	  from final;
quit;

%let colour = %str(red|green|blue);

ods graphics on / reset=all imagefmt=png outputfmt=PNG border=off height=100% width=100% antialias=on imagename="hemoglobin";
   
   proc sgplot data=final noautolegend;
       vbox aval / category=avisitn datalabel=usubjid boxwidth=1;
	%do i = 1 %to &n.;
	     %let col&i = %scan(&colour,&i,'|');
            refline &&minref&i &&maxref&i / axis=y lineattrs=(pattern=shortdash color=&&col&i);
	%end;
      yaxis type=linear values=(&minval. to &maxval. by &byvar.) label=%sysfunc(compbl("&param."));
      xaxis type=linear values=(1 2 3 4 5 6 7) label="Visit" /*offsetmin=0.1 offsetmax=0.1*/;
   run;

ods graphics off;

%mend labplot;

%labplot(paramcd = HGB, minval=100, maxval=180, byvar=10);

hemoglobin.png

SAS Super FREQ
Posts: 1,139

Re: Proc SGplot - formatting X axis

If you can send a simple program with built in data (like my example) that shows the problem, you have a better chance to get more help from the community.  With a program using macros and data in pdf, it is much harder to help debug the issue.

Contributor
Posts: 24

Re: Proc SGplot - formatting X axis

Without using the macro, I'm getting what I want. It's only when I use the macro, the offset problem occurs. Anyway using offsetmin/max=0.1 solve my problem.

Thanks for the solution. I'll be mindful about the data while I ask question next time.

Super User
Posts: 11,114

Re: Proc SGplot - formatting X axis

Run the code with options MPRINT symbolgen;

compare the log of the run with the code you have that generated the desired graph  before you added any macro variables.

 

Did you by any chance generate an earlier version of the graph of OFFSETMAX on the XAXIS and not replace the actual output file for some reason such as open in another program to view the result?

 

You may want to look at your ODS Graphics options and ODS PDF.

 

 

Super User
Posts: 19,058

Re: Proc SGplot - formatting X axis

I've edited your post - added the code using a code box and formatted it, as well as embedded the image using the PHOTO option. You can attach images with the photo instead of attachments. JPG/PNG are accepted formats among others.
Contributor
Posts: 24

Re: Proc SGplot - formatting X axis

Thanks
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 318 views
  • 1 like
  • 5 in conversation