BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

I have a list of variables in a data set.

Lets call them A1B, A2B..... A100B;

I need to write a do loop such that i can use the iteration value to call the variable.

data dataset;
set dataset;

counter = 0;
do i = 1 to 100;
if A&iB = etc... (how do i call the variable passing the iteration value)
counter+1;
end;

run;
6 REPLIES 6
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Explore using an ARRAY in your DATA step, where you can list the variables contained in the array and you would use an "index variable" to iterate through the variable list without having to name each variable within your DO/END code.

Scott Barry
SBBWorks, Inc.

Recommended Google advanced search argument this topic/post:

data step array site:sas.com
RickM
Fluorite | Level 6
A&i.B should resolve to the correct variable I think.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
To the OP: you cannot (normally) mix SAS macro language elements with DATA step code. If you add this SAS statement to your code, you will get more diagnostic information showing how/when the macro code is compiled and when the DATA step code is compiled and executed.

OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN MPRINT;

...and for DATA step diagnostics, add...

PUTLOG '>I AM HERE>' / _ALL_;

Scott Barry
SBBWorks, Inc.

Recommended Google advanced search argument this topic/post:

macro language basics introduction site:sas.com
hyqwel
Calcite | Level 5
After declare i as a macro variable first.

%let i=100;
...
a&i.b=...
....
RickM
Fluorite | Level 6
Correct,

I overlooked that this was in a data step and not macro code when I saw the &.
Cynthia_sas
SAS Super FREQ
Hi:
Although there are many instances where you might use a macro %DO loop, when generating code, it does not seem necessary to make this code task more difficult by introducing macro variables into the mix. It will only complicate debugging the logic and output should things not work as expected.

Consider the program below:
[pre]
data usearray;
set sashelp.class;
array silly a1b a2b a3b a4b a5b;
do i = 1 to 5 by 1;
silly(i) = age +i;
end;
run;

ods listing;
proc print data=usearray;
var name age a1b a2b a3b a4b a5b;
run;
[/pre]


In my program, above, the ARRAY statement declares an ARRAY named SILLY to hold 5 variables: A1B through A5B. The variables do NOT have to be similarly named, they must only be of the same type (all numeric or all character). A SAS ARRAY is just a convenient method for referencing a group of variables.

Then I have a regular DATA step DO loop, iterate from 1 to 5. I could have used the DIM function; I could have used a DO WHILE or a DO UNTIL loop (but then I would have been responsible for iterating the counter or index variable on my own).

The statement: silly(i) = age +i;
shows the referencing method that you can use AFTER an ARRAY has been declared in a program. When the value of I is 1 then silly(i) refers to the first variable in the ARRAY list, A1B, when the value of I is 2 then silly(i) refers to the second variable in the ARRAY list...and so on.

There are many other reasons for using a macro %DO loop or using macro variable references in a program. However, a simple &i will not iterate on it's own
-AND- a simple &i reference is probably inappropriate in a DATA step program because of issues with DATA step compile time, DATA step execution time, macro code resolution time and when you want your macro variable to resolve. Simple &MACVAR references (such as &I or &A) resolve at DATA step compile time AND THEN DO NOT CHANGE. So these simple &MACVAR references are only good for providing constant values, such as data set names, constant multipliers (used as constants in the program) or items that change for every iteration of the program, but stay the same for every observation in the data set.

As I said, a SAS Array is a very simple way of referencing a collection of variables. For example, if you wanted to check ALL the numeric variables in your data and change missing values to 0, you would do something like this:
[pre]
array allnum _numeric_;
do i = 1 to dim(allnum);
if allnum(i) = . then allnum(i) = 0;
end;
[/pre]

A technique like this saves you coding time, because you do NOT have to name all the variables in separate IF statements. There are other naming constructs you can use with your variable lists and the SAS documentation outlines all the different ways you can name and list variables.

If you needed to generate a PROC PRINT step 100 times or generate an IF statement 100 times, or generate a list of 1000 variable names -- to use in a program, those might be some of the instances where you would use a MACRO %DO loop inside a MACRO program. But you should know that a MACRO %DO loop is rarely a substitute for an ARRAY statement for simple iteration through a group of variables.

cynthia

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 13148 views
  • 0 likes
  • 5 in conversation