First it is normally not necessary to take values out of a dataset and into macro variables. Second look into using the QUOTE function. Taking the second issue first. You can use the QUOTE function to protect the value of your character variables from confusing SAS. This also has the advantage of generating a string that will be valid to compare to a character variable in your WHERE clause later. (A.STORE = "Walmart" instead of A.STORE = Walmart which would cause SAS to look for variable named WALMART.) (Nitpick alert. I changed your macro variables from STORESxxx to STORExxx as each value is for only one store) data _null_; if final then call symputx('count',_n_); set list end=final; call symputx('store'||left(_n_),quote(trim(stores)); run; Back to eliminating putting the values of dataset variables into macro variables. One way might be to use the FIRSTOBS and OBS dataset options to pull just one record from list at a time and merging it with source data. Again I have no idea why you are adding a variable called STORES to your output dataset when you already have a variable named STORE that by definition has the same value. %macro loop(ds1,ds2,prefix) ; %local i n ; proc sql noprint ; select count(*) into :n from &ds1 ; %do i=1 %to &n ; create table &prefix.&i as select a.* from &ds2 a , &ds1 (firstobs=&i obs=&i) b where a.store = b.store ; %end; quit; %mend loop; %loop(ds1=list,ds2=sample,prefix=test)
... View more