Write and run SAS programs in your web browser

Create consecutive table in SAS

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Create consecutive table in SAS

Hi, I'm trying to create a counting variable in PROC SQL to give name to tables. 

 

PROC SQL;

i=1;

CREATE TABLE i AS SELECT* FROM A.A WHERE XX=i;

RUN;

 

I can not declarete the variable i to make it interactive. 

 


Accepted Solutions
Solution
‎09-19-2016 10:48 AM
Esteemed Advisor
Posts: 6,646

Re: Create consecutive table in SAS

%macro make_datasets(max_i);
%do i = 1 %to &max_i;
data a.a_&i;
set a.a (where=(XX=&i));
run;
%end;
%mend;
%make_datasets(100);
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Grand Advisor
Posts: 17,323

Re: Create consecutive table in SAS

The usual recommendation is don't do this...if you specify why, send can usually offer a more efficient alternative. 

 

Otherwise You want a macro loop. 

 

http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

 

How to split a table by groups. 

http://blogs.sas.com/content/sasdummy/2015/01/26/how-to-split-one-data-set-into-many/

 

Super User
Super User
Posts: 6,318

Re: Create consecutive table in SAS

[ Edited ]

You need to use macro language to generate code.

%let id=100 ;
%let inds=a.a ;


proc sql ;
   create table x&id as 
     select * 
     from &inds
     where id = &id
   ;
quit;
Contributor
Posts: 23

Re: Create consecutive table in SAS

Thanks, it was just the sintaxsis.. but I just forget said I want a loop. I change my code for this:

 

%LET i=1;

 

DATA A.A_&i;

SET A.A(WHERE=(XX=&i));

RUN;

 

But I need, i=i+1 

 

%LET i=1;

DATA A.A_&i;
SET A.A(WHERE=(XX=&i));

i=i+1
RUN;

 

Solution
‎09-19-2016 10:48 AM
Esteemed Advisor
Posts: 6,646

Re: Create consecutive table in SAS

%macro make_datasets(max_i);
%do i = 1 %to &max_i;
data a.a_&i;
set a.a (where=(XX=&i));
run;
%end;
%mend;
%make_datasets(100);
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 369 views
  • 0 likes
  • 4 in conversation