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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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