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
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)
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)
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
Thank you all for the help. I guess I will need a way to uniquely name my temporary datasets.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.