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));
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;
See if the above link is helpful
Remove the quotes. Remember it needs to become valid SAS code, and the quotes are not valid..
set mylib.first_&value;
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.
@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
Thank you!
So I guess the question is why I get an error message, then 🙂 😞
what is this mylist.first22 libname?
and is
%mtaqa different maco definition other than aaa?
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
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;
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 🙂
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.