BookmarkSubscribeRSS Feed
User12
Obsidian | Level 7

Hi Guys,

 

I have a quick question can we create macro variables using :INTO clause in subqueries

 

select distinct names into :names1-:names&total from
(select names,Count(distinct names) into :total  from sample)

 

can we use :into in  a subquery as shown above

2 REPLIES 2
novinosrin
Tourmaline | Level 20

The answer is NO.

RTM-->

The INTO clause for the SELECT statement can assign the result of a calculation or the value of a data column (variable) to a macro variable. If the macro variable does not exist, INTO creates it. You can check the PROC SQL macro variable SQLOBS to see the number of rows (observations) produced by a SELECT statement.
The INTO clause can be used only in the outer query of a SELECT statement and not in a subquery. The INTO clause cannot be used when you are creating a table (CREATE TABLE) or a view (CREATE VIEW).
Macro variables created with INTO follow the scoping rules for the %LET statement. 
Values assigned by the INTO clause use the BEST8. format.
ballardw
Super User

As @novinosrin says, no.

But the "fix" is insted of subquery create a separate query that generates just the macro variables you attempt from the subquery. But that may not be needed at all.

proc sql noprint;
   select distinct names into :names1-:names9999
   from sample
   ;
quit;

 

If you do not 9999 distinct names on the names1-namesXXX are created.

The automatic SAS macro variable &sqlobs will have the count of returned items in the last previous query.

So

%let NameCount = &sqlobs;

immediately after the Proc SQL will capture that count.

If you suspect you need more than 9999 macro variables (a real bad sign IMHO) increase the count.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 1229 views
  • 2 likes
  • 3 in conversation