- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi Guys ,
I'm new to sas programming and now im trying to understand the sas program wriiten by someone -
can you please give me idea how below statment works ?
%do i=1 %to 60 %by 1;
%do j=&i-1 %to &i-1;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
First of all, this is not a do loop, it is a %do loop, a very important distinction.
%do means this is a macro statement, and it is executed by the macro preprocessor, and is used to create dynamic SAS code.
(think of the C preprocessor, if you are familiar with programming and the C language)
%do i=1 %to 60 %by 1;
will create a macro variable i (later to be referenced by &i), assigns it the value 1, execute what is inside the block up to %end;, increase i by 1 and repeat until i exceeds 60
Similarly,
%do j=&i-1 %to &i-1;
will iterate from i-1 to i-1, so it will do only one iteration.
Caution: the macro preprocessor knows only one datatype, namely text. That the calculation &i-1 is possible is a capability of the iterative %do macro statement, usually one would need to use %eval(&i-1) to do calculations with macro variables.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
When you see the % as part of the code (like: %do, %to, %macro, %mend, %if etc.)
the code is part of a macro program. A macro program is a kind of program generator.
The code you are looking at includes, probably, next lines:
$macro <macro_name> (argument1, argument2,...); /* start definition of a macro program */
.....
%do i=1 %to 60 %by 1;
%do j=&i-1 %to &i-1;
...... more code lines ...
%end; /* ending inner loop */
%end; /* ending outer loop */
..........
%mend <macro_name>; /* ending definition of a macro program */
%macro_name(...arguments...); /* execution of the macro program */
so, the two lines you showd is actaullay producing similar code, again and agian, with slight modification
according to i counting from 1 to 60 (are there 60 segemnet or variables ?), maybe relating to the previous one (as j=&i-1);
i and j in this case, in a macro program, are called macro variables. You relate to macro variable by adding prefix & before the name, that is: &i, &j etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The second loop
%do j=&i-1 %to &i-1;
seems rather useless as it only performs one iteration.
It could be replaced by a simple affectation :
%let j=&i.-1;
or
%let j=%eval(&i.-1)
The difference between both commands is that, with the first, whenever &j. is encountered in
the program, there will only be a subtitution for the current value of i and no evaluation of
the resulting expression, e.g., if &i.=10, &j. will be replaced by 10-1 wich may not be what we want
in certain contexts. Using %eval will ensure that the resulting expression will be evaluated so that
each occurrence of &j will be replaced by the value of &i.-1 (9 in the example).