BookmarkSubscribeRSS Feed
hayely_bristine
Calcite | Level 5

Hey everyone!

I am self-learning SAS and trying to do some exercises.  What I am trying to do is make a macro function to plot a graph.  Let's say, I want to plot the graph tanx on the interval 0 to 8pi.  Is this possible?  Does anyone know how to do this?

Thanks! 

7 REPLIES 7
ballardw
Super User

Do you have a data set?

What type of graph?

 

And why a "macro"?  SAS macros generate code. Many times there is no need to use a macro. The main reason would be to reuse significant amounts of code with different data sets and/or variables.

hayely_bristine
Calcite | Level 5

Hello!  Thanks for the quick reply.  No, I do not have any data set.  This is just an exercise I found online.  Basically, it is just to practice with macros  I am trying to make a function that will plot a function over some interval.  An example would be something like tanx on 0 to 4pi, or 0 to 12pi (over any interval I specify) that would just produce an output of a graph of that function. 

ballardw
Super User

First step: get some relevant data. SAS won't produce graphs without data.

 

If you want to graph a function or formula then that would be data step code, likely involving a do loop with your independent (x axis) variable as the loop control and create a y value for each. Something like

data tograph;
   do x= -10 to 10 by 0.1;
       y= 2*x*x;
       output;
   end;
run;

If you have something like that the typical plots would be SERIES or SCATTER but others might apply. SAS Procs SGPLOT and SGPanel can create dozens of different plots. Some make more sense than others for specific data.

Generic:

proc sgplot data=tograph;
  series x=x y=y;
run;

Statements like XAXIS or YAXIS modify the default appearance of the axis.

 

The first step in macro development is to get code working that does not involve macro code or macro variables anywhere. Then you decide which pieces make sense to "make dynamic" or whatever you concept of macros would be.

 

 

hayely_bristine
Calcite | Level 5

Ah, ok.  So if I wanted to use something with macros I can put the %let command to specify the interval/function, correct?

ballardw
Super User

@hayely_bristine wrote:

Ah, ok.  So if I wanted to use something with macros I can put the %let command to specify the interval/function, correct?


Perhaps. It may depend on the exact "function" you want to use and the parameters required. Depending on values and intervals you may run into issues regarding machine precision of storage. Hint: intervals like 1E-16 are below the precision SAS is likely to use.

 

 

Rick_SAS
SAS Super FREQ

The function f(x)=tan(x) is discontinuous and has infinite range on the domain [0, 8*pi], so let's use cos(x) to illustrate your request:

%let min = 0;
%let max = 8*constant('pi');
%let numSteps = 201;
data Cos;
pi = constant('pi');
do x = &min to &max by (&max-&min)/&numSteps;
   y = cos(x);
   output;
end;
run;

proc sgplot data=Cos;
   series x=x y=y;
   refline 0 / axis=y;
run;

You can handle discontinuous functions, too, but the extra work will be distracting if your goal is to learn the fundamentals of graphing a function in SAS

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 1544 views
  • 0 likes
  • 4 in conversation