data table; format id ser; input id ser; datalines; 1 12345 2 06789 ; run; data names(keep = id); set table; run; data _null_; set names nobs = n; call symput ('cnt', n); run; %macro test; %do i = 1 %to &cnt; data first; set names (obs = 1); run; data _null_; set first; call symput ('id', trim(id)); run; data run_"&id"; set table; addone = &id + 1; run; data names; set names (firstobs = 2); run; %end; %mend test; %test;
When I execute this SAS program, I get:
ERROR: The value '2'n is not a valid SAS name.
I tried run_"&id", run_'&id', run_&id, and options validvarname = any but nothing seems to quell the problem. What can I do?
As others have indicated, you do need to remove the double quotes. But there is more that can be cleaned up here. Try it this way:
%macro test;
%local i id;
%do i=1 %to &cnt;
data _null_;
set names (obs=&i firstobs=&i);
call symputx('id', id);
run;
data run_&id;
set table;
addone = &id + 1;
run;
%end;
%mend test;
Note: Added the %end statement.
Try
call symput ('id', put(id,z6.));
with run_&id
I dont understand. Why would you change the name to idd and then use run_id with only one d?
It is just typo... sorry
Remove the quotes.
data run_&id;
Removing the quotes produces:
NOTE: Line generated by the macro variable "ID". 1 run_ 2 - 22 200 ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, /, ;, _DATA_, _LAST_, _NULL_. ERROR 200-322: The symbol is not recognized and will be ignored.
Remove the quotes.
data run_&id;
As others have indicated, you do need to remove the double quotes. But there is more that can be cleaned up here. Try it this way:
%macro test;
%local i id;
%do i=1 %to &cnt;
data _null_;
set names (obs=&i firstobs=&i);
call symputx('id', id);
run;
data run_&id;
set table;
addone = &id + 1;
run;
%end;
%mend test;
Note: Added the %end statement.
Replace all of your 'call symput' with 'call symputx', the latter removes blanks when dealing with numbers.
Using symputx, still produces the same problem.
Try this (tested):
data _null_;
set first;
call symput ('id', left(id));
run;
data run_&id;
set table;
addone = &id + 1;
run;
LEFT returns a character string with leading blanks moved to the end of the value.
TRIM removes trailing blanks.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.