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

Hello,

 

 

Can we use Retain Keyword in Macro ? I believe not, Just wanted to confirm, if not what would be it's alternative for Macro to do incremental addition

 

 

Regards,

Jaiganesh

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

@jaiganesh wrote:

Retain Keyword (...) what would be it's alternative for Macro to do incremental addition


Hello @jaiganesh,

 

The primary purpose of the RETAIN statement in the DATA step is to prevent certain variables from being reset to missing at the beginning of a new iteration of the DATA step (implied loop).

 

In a SAS macro:

  1. there is neither an implied loop nor a mechanism that automatically (iteratively) clears macro variables.
  2. Incremental additions (if any) would typically occur in a %DO loop and don't require anything similar to a RETAIN statement (which wouldn't be needed even in an analogous DATA step DO loop anyway).

Example:

%macro test;
%let s=0;
%do i=20 %to 40 %by 10;
  %let s=%eval(&s+&i);
%end;
%put &=s; /* result: 90 */
%mend test;

%test

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

May I ask where, when and why would the subject of Retain appear in a text processing facility that doesn't process PDV or in other words execution that's done by the datastep compiler.  If you answer the 3 W's , that will help.

Kurt_Bremser
Super User

You do not, I repeat NOT, process data with the macro preprocessor, which is there only for making code dynamic, if such is needed.

Your question therefore shows a complete cluelessness about the subject. You need to deepen your understanding of the Base SAS language quite a lot before you even think about macros.

Tom
Super User Tom
Super User

You can use a macro program to generate any SAS code (or any other text for that matter) so I don't see any reason why would have any trouble generating a RETAIN statement. Or for that matter use the word RETAIN as a the name of a variable or macro variable.

FreelanceReinh
Jade | Level 19

@jaiganesh wrote:

Retain Keyword (...) what would be it's alternative for Macro to do incremental addition


Hello @jaiganesh,

 

The primary purpose of the RETAIN statement in the DATA step is to prevent certain variables from being reset to missing at the beginning of a new iteration of the DATA step (implied loop).

 

In a SAS macro:

  1. there is neither an implied loop nor a mechanism that automatically (iteratively) clears macro variables.
  2. Incremental additions (if any) would typically occur in a %DO loop and don't require anything similar to a RETAIN statement (which wouldn't be needed even in an analogous DATA step DO loop anyway).

Example:

%macro test;
%let s=0;
%do i=20 %to 40 %by 10;
  %let s=%eval(&s+&i);
%end;
%put &=s; /* result: 90 */
%mend test;

%test

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 2416 views
  • 0 likes
  • 5 in conversation