Help using Base SAS procedures

What is the Base Different between SAS Program and Macro ?

Posts: 0

What is the Base Different between SAS Program and Macro ?

I am new to SAS , could you please explain me What is the Base Different between SAS Program and Macro ?

What is the Best Place in the web to get some beginners info for SAS Programing.

Posts: 8,745

Re: What is the Base Different between SAS Program and Macro ?

Here's a good reference on SAS Macros and what they are:

For SAS programming, you'll want to look at many of the beginner tutorials and papers written for various user group meetings and the SAS Global Forum (a Google search on "SAS Programming Beginner" should net you some useful papers.

As for the difference between a SAS Program and a SAS Macro program -- generally, you build your SAS Macro programs from working SAS programs. Consider the following program, composed of 3 steps:
ods listing;
proc print data=sashelp.class(obs=5);

proc print;

proc print data=sashelp.prdsale(obs=5);

The above program is composed of 3 PROC PRINT steps. Pretty much everything is the same in all 3 steps except for the name of the data= option and the name of the dataset in the SAS title.

So, what if there was a way to get SAS to TYPE the 3 PROC PRINT steps for me??? The SAS Macro Facility will do just that.

Using the SAS Macro facility I can write and then invoke a macro program that can generate a single statement or a whole step or a whole series of steps. First I have to have a working SAS program, so I understand where the macro facility can be used. Then I use that working SAS program as the basis to define my Macro program:
%macro prtdata(lib=, dsn=, obsno=5);
%let lib = %upcase(&lib);
%let dsn = %upcase(&dsn);
proc print data=&lib..&dsn(obs=&obsno);
title "&lib..&dsn";
%mend prtdata;

The above macro program is not running or executing anything. The %MACRO/%MEND statements only DEFINE code that should be assembled by the macro processor. Everyplace in the code where I have an &MACVAR reference (like &LIB, &DSN or &OBSNO), some substitution will take place when I invoke the macro program. To invoke the macro program, I use the program name PRTDATA preceded by a %:
options symbolgen mprint mlogic;
%prtdata(lib=sashelp, dsn=class, obsno=7);
%prtdata(lib=sashelp, dsn=shoes, obsno=2);
%prtdata(lib=sashelp, dsn=prdsale, obsno=3);

I have used keyword parameters for my macro program. So the first time I invoke the macro program, I am passing the keyword parameter LIB a value of SASHELP, the DSN parameter a value of CLASS and the OBSNO parameter a value of 7. In order for the macro processor to perform the substitution, however, the parameters must use an '&' in the front of the parameter name, which acts as a trigger for the substitution to take place.

When the macro program executes, there's some housekeeping that's going to take place (I'm upper-casing the lib and dsn parameters -- because that's how I want them to appear in the title.) Then the SAS macro processor will "type" the 1st PROC PRINT step for SASHELP.CLASS and the typed or resolved code will go straight to the SAS compiler and then to be executed. Next, the macro processor will be invoked a second time when the second %prtdata is encountered. The second PROC PRINT will be typed, compiled and executed. Then the third invocation will cause the third PROC PRINT to be typed, compiled and executed. The OPTIONS statement turns on the options that will echo the resolved code in the SAS log and show you how the macro variable references are being resolved. The SAS Macro facility only does the resolving and typing of the code. The regular SAS compile and execution phases happen as though you had typed and then submitted the code yourself.

SAS Programming and SAS Macro programming do have concepts in common, but I recommend that you understand your SAS procedures and processes first (without SAS Macro facility being involved) before you dive into SAS Macro processing.

Posts: 0

Re: What is the Base Different between SAS Program and Macro ?

Thank you Cynthia for your exceptional explanation, There are very few persons in the world are responsible for making this world as a noble and hospital place for breathing. I am saying DEFINITELY YOU ARE ONE OF THEM.

Thanks Oramcle.
Ask a Question
Discussion stats
  • 2 replies
  • 2 in conversation