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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1026 views
  • 0 likes
  • 3 in conversation