DATA Step, Macro, Functions and more

need help with do loop statement

Not applicable
Posts: 0

need help with do loop statement

I'm fairly new to SAS and very new to macros so I'm hoping I could get some help from fellow SAS users with much more knowledge and experience. I've written code to do exactly what I need, however, the code is based on a single unique variable. There are data sets within the code that are tied to the variable and some data sets merge based on the unique variable. The problem I have is there are 193 more unique variables I need to run this same exact code for.

I've heard of using %do loops within macros but I can't seem to get started without losing my mind. Would someone have some code for a beginner that would get me moving in the right direction?

I'm wondering if I would need to make my code a macro in itself, then create the %do loop macro statement for the unique variable. I can picture it in my head but I can't seem to put it into SAS code.

Any help is greatly appreciated! Many thanks in advance.
Super Contributor
Super Contributor
Posts: 3,176

Re: need help with do loop statement

Posted in reply to deleted_user
Have you looked at the SAS support technical reference and conference papers, which provide much "free" information and programming techniques / methods.

A Google advanced search such as the one below yields several candidate results:

macro do loop processing

Also, if you are looking to "generate code" from source SAS variable and member names, consider also you may look at using DICTIONARY tables with PROC SQL (or their complimenting SAS views in SASHELP.Vxxxxxxx to work with generating SAS code based on known input data-values (variables, SAS library members, external file names).

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search arguments, this topic / post:

macro language documentation

dictionary tables

macro generate sas code
Regular Contributor
Posts: 241

Re: need help with do loop statement

Posted in reply to deleted_user
I'm a fairly seasoned SAS user and much familiar with macros, so I am hoping I could help out fellow SAS users, but can't. I've heard of *mind reading* and I can't seem to perform it effectively to scan OP's. Any help is greatly appreciated! Many thanks in advance. :-)

Seriously, below is my best guess, which may be totally different from what you are trying to do:
/* create a data set with 194 vars */
data one;
retain v001-v194 1;

/* create the data set, some, to be merged in */
data some;
retain v001-v194 1 s 2;

/* merge one and some matching on each of v001, v002, ..., v194,
creating datasets merged001, merged002, ... merged194, respectively */
%macro merge(n=);
%local out v s;
%let out=merged&n;
%let v = v&n;
%let s = s&n;
data &out;
merge one some(keep=&v s rename=(s=&s));
by &v;
keep &v &s;
%mend merge;
%macro doMerges(upto=);
%local i;
%do i = 1 %to &upto;
%mend doMerges;


/* check */
proc print data=merged194 noobs;
/* on lst
v194 s194
1 2
SAS Employee
Posts: 58

Re: need help with do loop statement

Posted in reply to deleted_user
Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation