DATA Step, Macro, Functions and more

Insert the parameter of a macro at the beginning of a variable or dataset name

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 110
Accepted Solution

Insert the parameter of a macro at the beginning of a variable or dataset name

Hello,

I want to create variables and dataset with a macro. Part of the name of those is one of the parameter of this macro.

When the parameter is located at the end of the name, it works.

Example:

%macro test(year);

data work.bd&year;

variable&years=variable;

run;

%mend test;

%test(2007);

How can I do the same, but with the parameter at the beginningof the name? This doesn't work because the parameter is not recognise:

%macro test(year);

data work.&yearbd;

&yearvariable=variable;

run;

%mend test;

%test(2007);

Thanks


Accepted Solutions
Solution
‎12-10-2012 09:30 PM
Super User
Posts: 9,676

Re: Insert the parameter of a macro at the beginning of a variable or dataset name

The first letter of a dataset name or variable can't be a digit, it is illegal.

I don't understand why would you want do that.You can use a underline before it to make it legal.

or use options validvarname=any ; to make it happen.

options validvarname=any ;

%macro test(year);

data work._&year.bd ;

"&year.variable"n=1; output;

run;

%mend test;

%test(2007);

Message was edited by: xia keshan

View solution in original post


All Replies
Solution
‎12-10-2012 09:30 PM
Super User
Posts: 9,676

Re: Insert the parameter of a macro at the beginning of a variable or dataset name

The first letter of a dataset name or variable can't be a digit, it is illegal.

I don't understand why would you want do that.You can use a underline before it to make it legal.

or use options validvarname=any ; to make it happen.

options validvarname=any ;

%macro test(year);

data work._&year.bd ;

"&year.variable"n=1; output;

run;

%mend test;

%test(2007);

Message was edited by: xia keshan

Frequent Contributor
Posts: 110

Re: Insert the parameter of a macro at the beginning of a variable or dataset name

Thanks. In fact, the problem was the . between the two terms.

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 175 views
  • 0 likes
  • 2 in conversation