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

Hi everyone,

 

I'm reading the text book 'SAS certification prep guide Advanced programming for sas9' and in chapter 12 it has this macro which I'm having trouble comprehending.

 

%macro prtlast;

   %if &syslast ne _NULL_ %the %do;

   proc print data=syslast(obs=5);

      title "Listing of &syslast data set";

run;

%end;

%else

    %put No data set has been created yet.;

%mend;

 

proc sort data=sasuser.courses out=bydays;

   by days;

run;

%prtlast;

 

What I don't understand is why there is no &prtlast? and why prtlast is not set = to anything? where is the syslast macro come from? Why can you not just use a %let prtlast = 'work.dataset';?

 

Any help explaining what is happening with this macro would be appreciated.

 

Thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User


Basically to use an automatic Macro variable are you required to declare the macro step?

No. You can use an automatic variable outside of a macro, but this macro is using conditional logic to only execute certain PROCs or Data steps. 

 

For example, if the data set didn't exist using a standard PROC PRINT would create an error in your log. Using this code means that it checks if the data set exists and then print it. 

 

Logic:

  1. Check if a data set has been created, if it does:
    1. prints the data set
    2. If it doesn't, it prints a message to the log saying the data set doesn't exist. 

 

Is this the only way to accomplish this type of logic, No. There are many ways to do things in SAS, and this is just one way to execute conditional logic. 

View solution in original post

6 REPLIES 6
Reeza
Super User

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n18mk1d0g1j31in1q6chazvfseel.htm&docset...

 

SYSLAST is an automatic macro variable. It's for when you want to automatically reference the last data set used by default. 

 

Reeza
Super User

I suspect you made some typo's in transcribing the code, can you please verify it?

ballardw
Super User

&prtlast would reference a single macro named prlast.

 

You have defined macro programming with the %Macro prtlast ....

Such code is called for execution using the %macronameonthemacrodefinition; reference.

 

The purpose of macro code is often to automate an action. In this case to print the last used dataset (condition on one has been used in the current session).

 

%let prtlast = 'work.dataset'; would only assign the text "work.dataset" to a variable, it would not actually print anything. AND using that macro variable in proc print such as

proc print data=&prtlast;

run;

will generate errors.

Tom
Super User Tom
Super User

You are getting confused between the difference between a macro variable and an actual macro.  It is not helpful that many places will frequently use "macro" when they really mean "macro variable".  A macro variable is a name that stores a string of characters.  A macro is an executable program that you can call.

 

& is used as a trigger to indicate to the macro processor that the name of a macro variable follows.  To execute a macro you need to use the % to trigger to the macro processor that the name of a macro you want to call will follow.

 

The most common way to define a macro variable is to use a %LET statement. In a %LET statement you need to follow the name of the macro variable to be defined by an equal sign and then the value to be assigned.

 

You use the %MACRO statement to define a macro.  The name of the macro to be defined follows the %MACRO keyword in the statement.  The %MEND statement marks the end of the macro definition.  A macro definition can include many statements including conditional statements like %IF and %DO that you cannot use in "open" SAS code.

 

The macro variable SYSLAST is an example of an automatic macro variable that SAS will create.  Other examples you should know about would include the SYSDATE, SYSDATE9 and SYSTIME macro variables that will be set when your SAS session starts.  These are all examples of automatic macro variables that will always exist.  There are also other automatic macro variables that will be created based on the code that you run.  For example the SQLOBS macro variable will get set when you run a SELECT statement inside of a PROC SQL step.

Scott86
Obsidian | Level 7

Ok thanks guys I get it. So is the %macro prtlast; part of the code so they can execute the %if functions outside the data step or because they want to use &SYSLAST? Basically to use an automatic Macro variable are you required to declare the macro step?

Reeza
Super User


Basically to use an automatic Macro variable are you required to declare the macro step?

No. You can use an automatic variable outside of a macro, but this macro is using conditional logic to only execute certain PROCs or Data steps. 

 

For example, if the data set didn't exist using a standard PROC PRINT would create an error in your log. Using this code means that it checks if the data set exists and then print it. 

 

Logic:

  1. Check if a data set has been created, if it does:
    1. prints the data set
    2. If it doesn't, it prints a message to the log saying the data set doesn't exist. 

 

Is this the only way to accomplish this type of logic, No. There are many ways to do things in SAS, and this is just one way to execute conditional logic. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 6 replies
  • 1347 views
  • 1 like
  • 4 in conversation