BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dwsmith
Obsidian | Level 7
    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?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

10 REPLIES 10
mohamed_zaki
Barite | Level 11

Try 

call symput ('id', put(id,z6.));

with run_&id

dwsmith
Obsidian | Level 7

I dont understand. Why would you change the name to idd and then use run_id with only one d?

mohamed_zaki
Barite | Level 11

It is just typo... sorry

Reeza
Super User

Remove the quotes.

 

data run_&id;
dwsmith
Obsidian | Level 7

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.

 

Reeza
Super User

Remove the quotes.

 

data run_&id;
Astounding
PROC Star

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.

Haikuo
Onyx | Level 15

Replace all of your 'call symput' with 'call symputx', the latter removes blanks when dealing with numbers.

dwsmith
Obsidian | Level 7

Using symputx, still produces the same problem.

lxn1021
Obsidian | Level 7

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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 10 replies
  • 2432 views
  • 0 likes
  • 6 in conversation