BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasuser33
Calcite | Level 5

I am trying to loop a command over a list of nonsequential values. 

The command would ideally open two files, indexed by the values, so e.g first_22 and second_22; first_28 and second_28, etc.

and then, after some data manipulations, save the resulting file as FinalFile22, Finalfile29.

Unfortunately the following line produces an error. 

 

set mylib."first_&value";

 

I have tried various places to put the dots and the quotation marks, all of which eventually produce an error of some sort. 

The sketch of the code is below.

 

I would appreciate any help with this. 

 

%macro aaa(values); 

%let count=%sysfunc(countw(&values));
%do i = 1 %to &count;
%let value=%qscan(&values,&i,%str(,));


libname mylib '/project/this/data';


data FirstData;

set mylib."first_&value";


[...]


data SecondData;

set mylib."second_&value";


[...]


data project.FinalFile&value;

set Finalfile;run;


%end;

%mend;

%mtaq(%str(22,28,34));

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Don't add the macro quoting. It is confusing the parser into seeing an extra token.

If you are going to use the value as part of a dataset name then it cannot contain any characters that need to be macro quoted.

%let value=%scan(&values,&i,%str(,));

If you need to keep the macro quoting (why?) then you can use %unquote() to remove it.

set %unquote(mylib.first_&value);

Or use a name literal so that SAS knows it is one token.

set mylib."first_&value"n;

 

View solution in original post

11 REPLIES 11
Reeza
Super User

Remove the quotes. Remember it needs to become valid SAS code, and the quotes are not valid..

 

set mylib.first_&value;
sasuser33
Calcite | Level 5

Thank you, Reeza

If I do not put anything in quotes, I get the following error message

 

ERROR: File mylib.first.DATA does not exist.
NOTE: Line generated by the macro variable "VALUE".
849 mylist.first22
________
22
________
200

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, -, :, ;, CUROBS, END, INDSNAME, KEY,
KEYRESET, KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.

ERROR 200-322: The symbol is not recognized and will be ignored.

novinosrin
Tourmaline | Level 20

@Reeza's suggestion does work when i tested your code:

 

Log:
94 %macro aaa(values);
95
96 %let count=%sysfunc(countw(&values));
97
98 %do i = 1 %to &count;
99 %let value=%qscan(&values,&i,%str(,));
100
101 /*libname mylib '/project/this/data';*/
102
103 %put data FirstData;
104 %put set mylib.first_&value;
105
106
107 /*data SecondData;*/
108 /*set mylib."second_&value";*/
109 /*[...]*/
110 /**/
111 /*data project.FinalFile&value;*/
112 /*set Finalfile;run; */
113 %end;
114 %mend;
115 %aaa(%str(22,28,34))
data FirstData
set mylib.first_22
data FirstData
set mylib.first_28
data FirstData
set mylib.first_34

 

sasuser33
Calcite | Level 5

Thank you!

So I guess the question is why I get an error message, then 🙂 😞

novinosrin
Tourmaline | Level 20

what is this mylist.first22 libname

and is 

%mtaq

a different maco definition other than aaa? 

sasuser33
Calcite | Level 5

Yes, sorry about that

1) what is this mylist.first22 libname

It should be mylib.first22

 

2) No, the macro should be called 

%aaa

 

Sorry, since I'm not an expert programmer at all, I have been relying on some examples, hence the issues with copy-pasting

 

Again, I appreciate your help

 

 

Tom
Super User Tom
Super User

Don't add the macro quoting. It is confusing the parser into seeing an extra token.

If you are going to use the value as part of a dataset name then it cannot contain any characters that need to be macro quoted.

%let value=%scan(&values,&i,%str(,));

If you need to keep the macro quoting (why?) then you can use %unquote() to remove it.

set %unquote(mylib.first_&value);

Or use a name literal so that SAS knows it is one token.

set mylib."first_&value"n;

 

sasuser33
Calcite | Level 5

Thanks a lot, Tom 

Your suggestions fix this problem. 

In the meantime, for some reason the code does not loop through the list as I was hoping it would, and just does everything (correctly) for the first element of the list. 

But that's a separate problem and I'll try to see if I can solve it on my own 🙂

Reeza
Super User

Before you make your code a macro make sure your base case works.

Then add your macro variable parameters one at a time and test each one. 

Then you don't run into these issues. 

sasuser33
Calcite | Level 5
You are right, Reeza.
That was my approach but apparently the quoting messed things up.
Thank you for your advice.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 2389 views
  • 0 likes
  • 5 in conversation