BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
R_Win
Calcite | Level 5

data l2;
input id $;
cards;
c1
c2
c3
run;

proc sql;
select count(*) into:emailtblcnt from l2;
quit;

In this the macro varibale at a time should hold one value if we run this at one instance the macro ids should have one value. how can i loop it


%macro emicnt;
%do i=1 %to &emailtblcnt.;
proc sql;
select id into:ids from l2;
quit;
%put &ids;
%end;
%mend;

%emicnt;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

I don't know why would you do this.

data l2;
input id $;
cards;
c1
c2
c3
run;

proc sql;
select count(*) into:emailtblcnt from l2;
quit;

%macro emicnt;
%do i=1 %to &emailtblcnt.;
proc sql;
select id into:ids1-:ids&i from l2;
quit;
%let ids=&&ids&i ;
%put &ids;
%end;
%mend;

%emicnt;

The following is simple enough.

proc sql;
select id from l2;
select id into:ids1-:ids&sqlobs from l2;
quit;

Ksharp

View solution in original post

2 REPLIES 2
Astounding
PROC Star

R_Win,

Modify the first SQL before you even get to macro language.  Add another SELECT statement:

select strip(ids) into : email_list separated by ' ' from I2;

Then inside your macro you can loop:

%do i=1 %to &emailtblcnt;

   %let next_id = scan(&email_list, &i, %str( ));

%end;

This approach assumes that the individual values of ID will not contain an embedded blank.  If there are embedded blanks, it gets more complicated but can still be done.

Good luck.

Ksharp
Super User

I don't know why would you do this.

data l2;
input id $;
cards;
c1
c2
c3
run;

proc sql;
select count(*) into:emailtblcnt from l2;
quit;

%macro emicnt;
%do i=1 %to &emailtblcnt.;
proc sql;
select id into:ids1-:ids&i from l2;
quit;
%let ids=&&ids&i ;
%put &ids;
%end;
%mend;

%emicnt;

The following is simple enough.

proc sql;
select id from l2;
select id into:ids1-:ids&sqlobs from l2;
quit;

Ksharp

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1403 views
  • 0 likes
  • 3 in conversation