DATA Step, Macro, Functions and more

local SAS dataset within a macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

local SAS dataset within a macro

Hello all:

I am wondering whether there is a way to define local/global SAS dataset? I am writing a Macro within which several temporary datasets will be created and merged. I want those temporary datasets to be local within the Macro definition so that I wouldn't accidentally overwrite any existing global datasets with the same name. I know SAS distinguishes local/global Macro variable but I cannot find an analogy for datasets. Is there any way to do that in SAS, like local/global objects in other languages such as C++ or Java?

Thanks,

Peter


Accepted Solutions
Solution
‎07-16-2012 05:58 PM
Super User
Super User
Posts: 6,502

Re: local SAS dataset within a macro

Short answer is no.  But there is more than one way to skin a cat.

Probably the easiest way is to pass the names of the datasets you want to use as parameters to your macro call rather than hardcoding dataset names.

SAS uses libraries to store datasets. You could generate a new directory and point a new libref to it (sort of like building your own stack?).  You will need to have a way to generate a unique directory name.  Not sure if SAS has that, but if you are on unix there are tools for that.

If you do not specify a LIBREF when creating a dataset then it is in the WORK library.  If you use the WORK library then you could generate a unique dataset name using prefixes.

Note that SAS will automatically name datasets with DATA1, DATA2, ... if you do not specify a dataset name in your DATA statement.  You can use that create your unique dataset names.

Say you want to call to a macro needs three dataset names.  Make three empty datasets to generate some unique names and store the names into macro variables.

data ; run;

%let name1=&syslast;

data; run;

%let name2=&syslast;

data; run;

%let name3=&syslast;

%mymacro(ds1=&name1,ds2=&name2,ds3=&name3)

View solution in original post


All Replies
Solution
‎07-16-2012 05:58 PM
Super User
Super User
Posts: 6,502

Re: local SAS dataset within a macro

Short answer is no.  But there is more than one way to skin a cat.

Probably the easiest way is to pass the names of the datasets you want to use as parameters to your macro call rather than hardcoding dataset names.

SAS uses libraries to store datasets. You could generate a new directory and point a new libref to it (sort of like building your own stack?).  You will need to have a way to generate a unique directory name.  Not sure if SAS has that, but if you are on unix there are tools for that.

If you do not specify a LIBREF when creating a dataset then it is in the WORK library.  If you use the WORK library then you could generate a unique dataset name using prefixes.

Note that SAS will automatically name datasets with DATA1, DATA2, ... if you do not specify a dataset name in your DATA statement.  You can use that create your unique dataset names.

Say you want to call to a macro needs three dataset names.  Make three empty datasets to generate some unique names and store the names into macro variables.

data ; run;

%let name1=&syslast;

data; run;

%let name2=&syslast;

data; run;

%let name3=&syslast;

%mymacro(ds1=&name1,ds2=&name2,ds3=&name3)

Super User
Posts: 9,687

Re: local SAS dataset within a macro

Are you learning JAVA or C++ ?

As Tom said, you can't have local/global SAS dataset. SAS is not OOP language.

But as your required, Maybe dataset option gennum= is what you are looking for, which will generate lots of generation of original dataset, and keep the original dataset name.

GENNUM= Data Set Option

Specifies a particular generation of a SAS data set.

Ksharp

Occasional Contributor
Posts: 12

Re: local SAS dataset within a macro

Thank you all for the help. I guess I will need a way to uniquely name my temporary datasets.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 529 views
  • 3 likes
  • 3 in conversation