Hi to all and Happy new year!
One question: I have noticed that when I am using the "_data_" keyword in a data step, SAS automatically creates a name for the dataset.
How do I invoke the same command using macro programming, to control which dataset name (and dataset of course) is generated?
I want this functionality because in the end of the process flow (EG), I need to clean any data resources that I don't want to.
Best Regards,
Vasilios
@vfarmak wrote:
I tried to use the _data_ keyword in a let statement.
%let random_ds = _data_;But this does not work.
I haven't though tried to use it with sysfunc. I will give it a try
That will set the macro variable to the 6 character string _data_.
What did you WANT it to set the macro variable to?
Why not just let SAS determine the name and then use &SYSLAST?
data; run;
%let random_ds = &syslast ;
But the names are not RANDOM. They are SEQUENTIAL.
1 data; run; NOTE: The data set WORK.DATA1 has 1 observations and 0 variables. NOTE: DATA statement used (Total process time): real time 0.04 seconds cpu time 0.00 seconds 2 %let random_ds = &syslast ; 3 %put &=random_ds; RANDOM_DS=WORK.DATA1 4 data; run; NOTE: The data set WORK.DATA2 has 1 observations and 0 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 5 %let random_ds = &syslast ; 6 %put &=random_ds; RANDOM_DS=WORK.DATA2
So if you want to generate sequential names just make your own counter variable.
1 %let next_seq=1; 2 %let myprefix=pathA; 3 4 %let random_ds = &myprefix.&next_seq; 5 %let next_seq=%eval(&next_seq+1); 6 %put &=random_ds; RANDOM_DS=pathA1 7 8 %let random_ds = &myprefix.&next_seq; 9 %let next_seq=%eval(&next_seq+1); 10 %put &=random_ds; RANDOM_DS=pathA2
Then each of your parallel tracks can use their own prefix and avoid any dataset name conflicts.
If you are creating data sets in the WORK library by default all data sets in the Work library are cleared when the SAS session ends. So I don't see what name you want to reference. They would also be named Work.dataN where N is an integer. You could explicitly remove all of them using
Proc datasets library=work; delete data: ; run; quit;
So there is no need to "know" the exact names created. The colon after the Data means "delete all data sets whose name starts with "data".
They would also be named Work.dataN where N is an integer. You could explicitly remove all of them using
Proc datasets library=work; delete data: ; run; quit;
Note that this code will delete all data sets starting with DATA, such as DATA_FINAL_PLEASE_SAVE_ME, not just data sets named DATAn.
I want the SAS code to be a part of a SAS macro and combine in with fork and wait functionality provided in SAS Data Integration Studio.
If I have the same process running for lets say many records simultaneously, I don't one process to read or write to datasets of another process.
This is the main reason I want to have autogenerated names just like the drag and drop functionality in DI (when you actually drag an drop an extract component, the name of the table is a random string).
So you want to be able do delete all tables with the naming convention DATAn?
If you can stick to data steps in your macro it should work, or?
Otherwise you could have macro variable which you augment each time you create a new table. Depends on how your macro(s) are designed.
Exactly. Only the ones that I have defined (or the macro code defined).
I don't understand ... why would a data step behave differently when being part of a macro?
The '_data_' works identically outside and within a macro, no?
Cheers, Koen
I tried to use the _data_ keyword in a let statement.
%let random_ds = _data_;
But this does not work.
I haven't though tried to use it with sysfunc. I will give it a try
How do you mean it doesn't work?
If you use that macro variable in a data step, it should work, no?
Maybe you can share some of your macro code so we can see your logic.
@vfarmak wrote:
I tried to use the _data_ keyword in a let statement.
%let random_ds = _data_;But this does not work.
I haven't though tried to use it with sysfunc. I will give it a try
That will set the macro variable to the 6 character string _data_.
What did you WANT it to set the macro variable to?
Why not just let SAS determine the name and then use &SYSLAST?
data; run;
%let random_ds = &syslast ;
But the names are not RANDOM. They are SEQUENTIAL.
1 data; run; NOTE: The data set WORK.DATA1 has 1 observations and 0 variables. NOTE: DATA statement used (Total process time): real time 0.04 seconds cpu time 0.00 seconds 2 %let random_ds = &syslast ; 3 %put &=random_ds; RANDOM_DS=WORK.DATA1 4 data; run; NOTE: The data set WORK.DATA2 has 1 observations and 0 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 5 %let random_ds = &syslast ; 6 %put &=random_ds; RANDOM_DS=WORK.DATA2
So if you want to generate sequential names just make your own counter variable.
1 %let next_seq=1; 2 %let myprefix=pathA; 3 4 %let random_ds = &myprefix.&next_seq; 5 %let next_seq=%eval(&next_seq+1); 6 %put &=random_ds; RANDOM_DS=pathA1 7 8 %let random_ds = &myprefix.&next_seq; 9 %let next_seq=%eval(&next_seq+1); 10 %put &=random_ds; RANDOM_DS=pathA2
Then each of your parallel tracks can use their own prefix and avoid any dataset name conflicts.
I want to mimic the functionality that DI gives when you drag and drop an extract components which has an output table with a generated name.
I can see that your code provides this kind of functionality.
By using the below statements you have mentioned, might be the case and I will definitely give it a try.
data ;
run;
%let ds = &syslast;
I want to mimic the functionality that DI gives when you drag and drop an extract components which has an output table with a generated name.
I can see that your code provides this kind of functionality.
By using the below statements you have mentioned, might be the case and I will definitely give it a try.
data; run;
%let ds = &syslast;
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.