BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tradepeter
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Tom
Super User Tom
Super User

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)

Ksharp
Super User

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

tradepeter
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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