BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Have you looked at the SAS support http://support.sas.com/ 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 site:sas.com

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 site:sas.com

dictionary tables site:sas.com

macro generate sas code site:sas.com
chang_y_chung_hotmail_com
Obsidian | Level 7
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:
[pre]
/* create a data set with 194 vars */
data one;
retain v001-v194 1;
run;

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

/* 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;
run;
%mend merge;
%macro doMerges(upto=);
%local i;
%do i = 1 %to &upto;
%merge(n=&i)
%end;
%mend doMerges;

%doMerges(upto=194)

/* check */
proc print data=merged194 noobs;
run;
/* on lst
v194 s194
1 2
*/
[/pre]

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 657 views
  • 0 likes
  • 4 in conversation