DATA Step, Macro, Functions and more

Do loop with array

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Do loop with array

Hi, I want to create a do loop to increment the values of a variable in a sas dataset.

 

I am writing the following code:

 

Proc sql;

select Off into: Off_Var separated by ','

from Control_Table;

quit;

 

%do i=0 to 1;

%let Off_Mac= &Off_Var.&i;

%end;

 

I want to put the first value from Off_Var to Off_Mac when i=0, and put the second value of Off_Var to Off_Mac when i=1.

 

Please help, 

Thanks 


Accepted Solutions
Solution
‎04-19-2018 12:59 AM
Occasional Contributor
Posts: 17

Re: Do loop with array

Posted in reply to KurtBremser

Hi,

 

This code worked for me!

I am not sure what %bquote does tho.

 

Thanks anyways Smiley Happy

%do i=0 %to 1;
  %let Off_Mac= %scan(%bquote(&Off_Var),&i,%str(,));
%end;

 

View solution in original post


All Replies
Super User
Posts: 10,568

Re: Do loop with array

Posted in reply to katariasarthak

You will only get one macro variable out fo the SQL, so you need to dissect it:

%do i=0 %to 1;
  %let Off_Mac= %scan(&Off_Var,&i+1);
%end;

(assuming there's lots of more code in the macro loop, as you always end up with the second word after the loop)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎04-19-2018 12:59 AM
Occasional Contributor
Posts: 17

Re: Do loop with array

Posted in reply to KurtBremser

Hi,

 

This code worked for me!

I am not sure what %bquote does tho.

 

Thanks anyways Smiley Happy

%do i=0 %to 1;
  %let Off_Mac= %scan(%bquote(&Off_Var),&i,%str(,));
%end;

 

Super User
Posts: 10,568

Re: Do loop with array

Posted in reply to katariasarthak
Super User
Posts: 10,568

Re: Do loop with array

Posted in reply to katariasarthak

PS &off_var is not an array, it is what is usually called a list macro variable.

SAS arrays only exist in data step code.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,838

Re: Do loop with array

Posted in reply to katariasarthak

What is it your trying to do?  Really don't see the point in this.  I mean if you need to split that var into two then:

data _null_;
  set control_table;
  call symputx('off_mac1',scan(off,1,","));
  call symputx('off_mac2',scan(off,2,","));
run;

But that just seems to be a waste of coding time, if you already have the data in the dataset, you can simply merge that row onto the data you need to check and check then, hence avoiding all this messy code.  Providing test data in the form of a datastep and what you want to see at the end really helps clarify questions.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 127 views
  • 0 likes
  • 3 in conversation