Learning SAS? Welcome to the exclusive online community for all SAS learners.

How to assign value of one macro variable to another macro variable

Reply
Occasional Contributor
Posts: 17

How to assign value of one macro variable to another macro variable

Hello All,

I want to assign the value of "sorted_&dataset" macro variable to global macro variable "dataset_name" so that I can use this data set in other parts of the program.

following is my code:

%global dataset_name;

%macro sortingandprinting(by_variable =, dataset =);

       proc sort data = &dataset

         out = sorted_&dataset;

  by &by_variable;

    run;

    data _null_;

         dataset_name = sorted_&dataset;

    run;

%mend sortingandprinting;

Executing the Macro

%sortingandprinting(by_variable = jobcode, dataset = payrollmaster)

// While printing the following, the value is not updated even after executing the macro

%put dataset_name = &dataset_name;

Following is the output from the log:

dataset_name =

Please Help !

Super User
Super User
Posts: 7,942

Re: How to assign value of one macro variable to another macro variable

Posted in reply to gurpreetkaur

Well, you can do it in this way:

%global dataset_name;

%let dataset_name=class;

%let dataset_name=sashelp.&dataset_name.;

%put &dataset_name;

However I would ask why you want to go down this route.  I would have thought keeping the actual names in your main code rather than manipulating them within a macro would be advisable.  I.e. is there any value in doing a proc sort in macro code, that just obfuscates the code slightly for very little gain.

Occasional Contributor
Posts: 11

Re: How to assign value of one macro variable to another macro variable

Posted in reply to gurpreetkaur

%macro sortingandprinting(by_variable =, dataset =);

%global dataset_name;

%let dataset_name = sorted_&dataset;

       proc sort data = &dataset

         out = sorted_&dataset;

  by &by_variable;

    run;

  

%mend sortingandprinting;

Executing the Macro

%sortingandprinting(by_variable = jobcode, dataset = payrollmaster)

%put dataset_name = &dataset_name;

dataset_name = sorted_payrollmaster

Super User
Posts: 19,789

Re: How to assign value of one macro variable to another macro variable

Posted in reply to gurpreetkaur

1. Why not assign a global variable in the first place?

2. What do you expect the following portion of code to do:

    data _null_;

         dataset_name = sorted_&dataset;

    run;

Super Contributor
Posts: 308

Re: How to assign value of one macro variable to another macro variable

Posted in reply to gurpreetkaur

Hello,

options mprint symbolgen;


%macro sortingandprinting(by_variable =, dataset = , library=work);
%global dataset_name;

proc sort data = &library..&dataset out = sorted_&dataset;
by &by_variable;
run;

%let dataset_name=sorted_&dataset;

%mend sortingandprinting;

%sortingandprinting(by_variable = sex, dataset = class, library=sashelp)

Super User
Super User
Posts: 7,039

Re: How to assign value of one macro variable to another macro variable

Posted in reply to gurpreetkaur

One point is to avoid global macro variables if you don't need them.  Since you have a macro already you can use macro logic to define the new macro variable as global only if it does not already exist.

%if not %symexist(dataset_name) %then %global dataset_name;

To make the code maintenance easier you should probably set the macro variable first and then use it.


%macro sortingandprinting(by_variable =, dataset =);

%if not %symexist(dataset_name) %then %global dataset_name;

%let dataset_name=sorted_&dataset ;


proc sort data = &dataset out = &dataset_name;

  by &by_variable;

run;

%mend sortingandprinting;

Or you might want to look at the automatic macro variable SYSLAST.

%macro sortingandprinting(by_variable =, dataset =);

%if not %symexist(dataset_name) %then %global dataset_name;


proc sort data = &dataset out = sorted_&dataset;

  by &by_variable;

run;


%let dataset_name=&syslast ;


%mend sortingandprinting;

Ask a Question
Discussion stats
  • 5 replies
  • 557 views
  • 0 likes
  • 6 in conversation