DATA Step, Macro, Functions and more

Using a macro variable to name a datastep

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

Using a macro variable to name a datastep

    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?


Accepted Solutions
Solution
‎02-25-2016 11:16 AM
Super User
Posts: 5,082

Re: Using a macro variable to name a datastep

[ Edited ]

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


All Replies
Super Contributor
Posts: 490

Re: Using a macro variable to name a datastep

[ Edited ]

Try 

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

with run_&id

Contributor
Posts: 40

Re: Using a macro variable to name a datastep

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

Super Contributor
Posts: 490

Re: Using a macro variable to name a datastep

It is just typo... sorry

Super User
Posts: 17,819

Re: Using a macro variable to name a datastep

Remove the quotes.

 

data run_&id;
Contributor
Posts: 40

Re: Using a macro variable to name a datastep

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.

 

Super User
Posts: 17,819

Re: Using a macro variable to name a datastep

Remove the quotes.

 

data run_&id;
Solution
‎02-25-2016 11:16 AM
Super User
Posts: 5,082

Re: Using a macro variable to name a datastep

[ Edited ]

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.

Respected Advisor
Posts: 3,124

Re: Using a macro variable to name a datastep

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

Contributor
Posts: 40

Re: Using a macro variable to name a datastep

Using symputx, still produces the same problem.

Occasional Contributor
Posts: 17

Re: Using a macro variable to name a datastep

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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