Suppose you need to create a numbered series of macro variables, one macro variable per row, from an SQL query. Suppose you magically know in advance that a given WHERE clause returns, say, 123 rows. No problem!
proc sql;
select customer_name into :name1-:name123
from orion.customer
where country='DE';
quit;
Now suppose you do not know in advance how many rows a given WHERE clause will return.
proc sql;
select customer_name into :name1-:name???
from orion.customer
where country='DE';
quit;
In SAS 9.3, you can use a hyphen in the INTO clause to specify a range without an upper bound!
proc sql;
select customer_name into :name1-
from orion.customer
where country='DE';
quit;
If, later, you need to know the number of rows returned by the query, just reference the SQLOBS automatic macro variable.
%macro custnames;
%do i=1 %to &sqlobs;
%put &i.. &&name&i;
%end;
%mend custnames;
%custnames
Happy queries! (You can learn more about macros in SAS Macro Language 1: Essentials and SAS Macro Language 2: Advanced Techniques.)
6 Comments
Thank u its very easy to understanding n easily remembering
Hi
I want to know how we can make a SAS & SQL database for some specific table's connection.
Thanks
Sandeep
Greetings! Very useful advice within this post! It's the little changes that will make the greatest changes. Thanks for sharing!
Hello! I could have sworn I've been to this site before but after browsing through some of the post I realized it's new to me.
Anyhow, I'm definitely delighted I found it and I'll
be bookmarking and checking back often!
previously the Cute.Yet.Accurate (CYA) solution was:
into :name1-name&SysMaxLong.
This trick was noted by Fehd and Carpenter in their SGF.2007 paper
List Processing Basics.
http://www2.sas.com/proceedings/forum2007/113-2007.pdf
see also:
http://www.sascommunity.org/wiki/How_To_Use_Proc_SQL_select_into_for_List_Processing
Great tip! This technique is the basis for a common programming pattern for "roll your own" BY processing. More on this new 9.3 feature (plus some reader comments with other ideas) at this post.