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

Hi,

I have a gplot with x var* y var, to which I need to insert few dynamic data dependent vertical lines.  Need to show some events happened at different time points with a vertical dashed lines.  Any idea how to get those?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

The option you are looking for is to add HREF= to your Plot statement in the options  to set the locations and LHREF= to set the line type (anything other than 1 will be some sore to dot or dash line), and CHREF to set color. LHREF and CHREF also support lists so you can set different line type and color for each HREF entry if so desired.

 

HREF= (5, 8, 12)  would set vertical reference lines at the listed values, the syntax is basically the same as for an HAXIS statement.

The fun part, since you say dynamic is where you get the values from. Easiest would be create a data set with the desired values, your choice of methods and then build a macro string such as:

 

Proc sql noprint;

    select values into : hreflist separated by ','

    from hrefdata;

quit;

and then the href code would look like

 

HREF=(&hreflist)

 

 

View solution in original post

6 REPLIES 6
Rick_SAS
SAS Super FREQ

Most people use SQL or a DATA _NULL_ step to put the reference values into macro variables that you can then use to draw the reference lines. For example, the following code writes the Q1 and Q3 values of a variable to a data set and then creates macro variables with the values:

 

/* write quantiles to data set */
proc univariate data=sashelp.class;
var Weight;
ods output quantiles=Q;
run;
 
proc print data=Q;
run;

/* create macro variables for interquartile range */
data _null_;
set Q;
if Quantile="75% Q3" then call symput("Q3", Estimate);
if Quantile="25% Q1" then call symput("Q1", Estimate);
run;

%put &=Q1;
%put &=Q3;

You can then use these values in SAS procedures. For example, in PROC SGPLOT I would write:

proc sgplot data=sashelp.class;
   histogram weight;
   refline &Q1 &Q3 / axis=x;
run;
ballardw
Super User

The option you are looking for is to add HREF= to your Plot statement in the options  to set the locations and LHREF= to set the line type (anything other than 1 will be some sore to dot or dash line), and CHREF to set color. LHREF and CHREF also support lists so you can set different line type and color for each HREF entry if so desired.

 

HREF= (5, 8, 12)  would set vertical reference lines at the listed values, the syntax is basically the same as for an HAXIS statement.

The fun part, since you say dynamic is where you get the values from. Easiest would be create a data set with the desired values, your choice of methods and then build a macro string such as:

 

Proc sql noprint;

    select values into : hreflist separated by ','

    from hrefdata;

quit;

and then the href code would look like

 

HREF=(&hreflist)

 

 

DanH_sas
SAS Super FREQ

As an additional SGPLOT alternative, you can have the REFLINE reference a column of values for the reference line instead of using macor variables.

 

data airrefs;
ref='01jan1950'd;
output;
ref='01jan1960'd;
output;
run;

data air;
merge sashelp.air airrefs;
run;

proc sgplot data=air;
series x=date y=air;
refline ref / axis=x lineattrs=(pattern=dash);
run;
Ram_SAS
Obsidian | Level 7
Thanks for the suggestions. It is not just one plot. I need to create multiple plots (currently using run-group processing) , each plot should represent each subject. So each subject will have multiple data dependent vertical lines.
DanH_sas
SAS Super FREQ

If you are using a BY variable for the subject, you can match-merge the reference line data to the original data by subject (if the ref data is not already in the original data) and reference the reference line  data from the REFLINE statement.

rogerjdeangelis
Barite | Level 11
Dynamic data dependent vertical lines

Not sure this is what the op wanted.
It pre-supposes the op wanted to enter some values.

see
https://goo.gl/l7zMKo
https://communities.sas.com/t5/SAS-GRAPH-and-ODS-Graphics/dynamic-data-dependent-vertical-lines/m-p/344098


HAVE
====

Up to 40 obs from sashelp.class total obs=19

Obs   HEIGHT    WEIGHT

  1    69.0      112.5
  2    56.5       84.0
  3    65.3       98.0
  4    62.8      102.5
  5    63.5      102.5
  6    57.3       83.0
  7    59.8       84.5
  8    62.5      112.5
  9    62.5       84.0
 10    59.0       99.5
 11    51.3       50.5
 12    64.3       90.0
 13    56.3       77.0
 14    66.5      112.0
 15    72.0      150.0
 16    64.8      128.0
 17    67.0      133.0
 18    57.5       85.0
 19    66.5      112.0


WANT
====

Pop up window

+--------------------------+
|                          |
|  Enter q1: _84<enter>__  |
|  Enter q3: _112<enter>_  |
|                          |
+--------------------------+


WEIGHT
150 +                        A
    |
    |
    |                   A
    |                A
125 +
    |
    |              A      A
    |------------------B---------------  84
    |              AA
100 +          A      A
    |
    |                A
    |-------AB--A--A------------------- 112
    |
 75 +       A
    |
    |
    |
    |
 50 + A
    -+----------+----------+----------+
    50         60         70         80

                   HEIGHT

FULL SOLUTION
=============

%symdel q1 q3 / nowarn;
options missing=' ';
data _null_;

   length q1 q3 $10;
   set mty;
   if _n_=1 then do;
      window start irow=1 rows=12 color=white
             #3 @10  'Enter q1:' +1 q1 $10. attr = underline
             #5 @10  'Enter q3:' +1 q3 $10. attr = underline;
      display start;
   end;

   call symputx('q1',q1);
   call symputx('q3',q3);

   rc=dosubl('
     options ls=64 ps=32;
     proc plot data=sashelp.class(keep=height weight);
     plot weight*height /vref=&q1 &q3;
     run;quit;
   ');

run;quit;


1885  %symdel q1 q3 / nowarn;
1886  options missing=' ';
1887  data _null_;
1888     length q1 q3 $10;
1889     set mty;
1890     if _n_=1 then do;
1891        window start irow=1 rows=12 color=white
1892               #3 @10  'Enter q1:' +1 q1 $10. attr =
1892! underline
1893               #5 @10  'Enter q3:' +1 q3 $10. attr =
1893! underline;
1894        display start;
1895     end;
1896     call symputx('q1',q1);
1897     call symputx('q3',q3);
1898     rc=dosubl('
1899       options ls=64 ps=32;
1900       proc plot data=sashelp.class(keep=height weight);
1901       plot weight*height /vref=&q1 &q3;
1902       run;quit;
1903     ');
1904  run;

SYMBOLGEN:  Macro variable Q1 resolves to 80
SYMBOLGEN:  Macro variable Q3 resolves to 112
NOTE: There were 19 observations read from the data set
      SASHELP.CLASS.
NOTE: PROCEDURE PLOT used (Total process time):
      real time           0.03 seconds
      user cpu time       0.00 seconds
      system cpu time     0.03 seconds
      memory              967.65k
      OS Memory           16876.00k
      Timestamp           03/24/2017 01:26:34 PM
      Step Count                        170  Switch Count  0


NOTE: There were 1 observations read from the data set WORK.MTY.
NOTE: DATA statement used (Total process time):
      real time           5.90 seconds
      user cpu time       0.04 seconds
      system cpu time     0.09 seconds
      memory              967.65k
      OS Memory           16876.00k
      Timestamp           03/24/2017 01:26:34 PM
      Step Count                        170  Switch Count  9

1904!     quit;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2404 views
  • 4 likes
  • 5 in conversation