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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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