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
Hi,
This code worked for me!
I am not sure what %bquote does tho.
Thanks anyways 🙂
%do i=0 %to 1;
%let Off_Mac= %scan(%bquote(&Off_Var),&i,%str(,));
%end;
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)
Hi,
This code worked for me!
I am not sure what %bquote does tho.
Thanks anyways 🙂
%do i=0 %to 1;
%let Off_Mac= %scan(%bquote(&Off_Var),&i,%str(,));
%end;
from the Macro Language Reference
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.
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.
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!
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.