DATA Step, Macro, Functions and more

Reg:Macro Looping i wnat one obs in that macro

Accepted Solution Solved
Reply
Regular Contributor
Posts: 229
Accepted Solution

Reg:Macro Looping i wnat one obs in that macro

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;

Attachment

Accepted Solutions
Solution
‎08-29-2012 12:55 AM
Super User
Posts: 9,662

Re: Reg:Macro Looping i wnat one obs in that macro

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


All Replies
Super User
Posts: 5,069

Re: Reg:Macro Looping i wnat one obs in that macro

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.

Solution
‎08-29-2012 12:55 AM
Super User
Posts: 9,662

Re: Reg:Macro Looping i wnat one obs in that macro

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

☑ This topic is SOLVED.

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

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