Desktop productivity for business analysts and programmers

Macros

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

Macros

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

 


Accepted Solutions
Solution
‎01-11-2018 02:08 PM
Super User
Posts: 22,874

Re: Macros



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


All Replies
Super User
Posts: 22,874

Re: Macros

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. 

 

Super User
Posts: 22,874

Re: Macros

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

Super User
Posts: 13,084

Re: Macros

&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.

Super User
Super User
Posts: 7,860

Re: Macros

[ Edited ]

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.

Contributor
Posts: 26

Re: Macros

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?

Solution
‎01-11-2018 02:08 PM
Super User
Posts: 22,874

Re: Macros



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. 

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 279 views
  • 1 like
  • 4 in conversation