Dear all,
I have a library named TEST where all datasets names have a common suffix _DRT i.e.
First_DRT
Second_DRT
Third_DRT
etc...
I need to rename all of the datasets to
First
Second
Third
etc.
without the common suffix
Any advice would be more than welcome
Thanking you in advance
I would build a SAS program (daa step) that reads SASHELP,VTABLE, filtering on your suffix.
Then (in the data step) generate dynamically PROC DATASETS code for renaming, using CALL EXECUTE.
@LinusH wrote:
I would build a SAS program (daa step) that reads SASHELP,VTABLE, filtering on your suffix.
Then (in the data step) generate dynamically PROC DATASETS code for renaming, using CALL EXECUTE.
AFTER verifying that there would no duplicate resulting data set names.
One of the things about suffixes is they are often used to differentiate between similar items. So what you also have a data set named First_XYZ? Has this been determined before starting this process (for @Zeus_Olympus )
Yes, indeed.
It is a fixed and known suffix (_DRT) before any coding.
If this was all in WORK, this is how I would do it. Note this works for a smaller amount of data sets (a few hundred). If the macro variable gets too big then a data step/call execute approach is appropriate instead.
*delete fake data to avoid issues;
proc datasets nodetails nolist;
delete first second third fourth_multi fifth_drthtest;
quit;
*create fake data for testing;
data first_drt;
set sashelp.class;
data second_drt;
set sashelp.class;
data third_drt;
set sashelp.class;
run;
data fourth_multi_drt;
set sashelp.class;
run;
data fifth_drthtest_drt;
set sashelp.class;
run;
*create a macro variable with the new and old names;
proc sql NOPRINT;
select catx("=", /*separator between name and new name*/
memname, /*original name*/
substr(memname, 1, length(memname)-4) /*new name, 4 is length of suffix to remove*/
) into :rename_list separated by " "
from sashelp.vtable
where libname='WORK' and memname like '%_DRT';
quit;
*display macro variable for confirmation;
%put &rename_list.;
options symbolgen;
proc datasets lib=work nodetails nolist;
change &rename_list;
quit;
options nosymbolgen;
Log:
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
SYMBOLGEN: Macro variable _SASWSTEMP_ resolves to /home/fkhurshed/.sasstudio/.images/d3a117db-e70b-4b05-aa58-1ef8b16a1397
SYMBOLGEN: Some characters in the above value which were subject to macro quoting have been unquoted for printing.
SYMBOLGEN: Macro variable GRAPHINIT resolves to GOPTIONS RESET=ALL GSFNAME=_GSFNAME;
68
69 *delete fake data to avoid issues;
70 proc datasets nodetails nolist;
71 delete first second third fourth_multi fifth_drthtest;
72 quit;
NOTE: Deleting WORK.FIRST (memtype=DATA).
NOTE: Deleting WORK.SECOND (memtype=DATA).
NOTE: Deleting WORK.THIRD (memtype=DATA).
NOTE: Deleting WORK.FOURTH_MULTI (memtype=DATA).
NOTE: Deleting WORK.FIFTH_DRTHTEST (memtype=DATA).
NOTE: PROCEDURE DATASETS used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 362.18k
OS Memory 25252.00k
Timestamp 10/11/2022 11:42:16 PM
Step Count 156 Switch Count 10
Page Faults 0
Page Reclaims 57
Page Swaps 0
Voluntary Context Switches 45
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 8
73
74 *create fake data for testing;
75 data first_drt;
76 set sashelp.class;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.FIRST_DRT has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 685.21k
OS Memory 25508.00k
Timestamp 10/11/2022 11:42:16 PM
Step Count 157 Switch Count 2
Page Faults 0
Page Reclaims 124
Page Swaps 0
Voluntary Context Switches 10
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 264
77 data second_drt;
78 set sashelp.class;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.SECOND_DRT has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 684.84k
OS Memory 25508.00k
Timestamp 10/11/2022 11:42:16 PM
Step Count 158 Switch Count 2
Page Faults 0
Page Reclaims 90
Page Swaps 0
Voluntary Context Switches 10
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 264
79 data third_drt;
80 set sashelp.class;
81 run;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.THIRD_DRT has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.01 seconds
system cpu time 0.00 seconds
memory 685.21k
OS Memory 25508.00k
Timestamp 10/11/2022 11:42:16 PM
Step Count 159 Switch Count 2
Page Faults 0
Page Reclaims 88
Page Swaps 0
Voluntary Context Switches 10
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 264
82 data fourth_multi_drt;
83 set sashelp.class;
84 run;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.FOURTH_MULTI_DRT has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 685.21k
OS Memory 25508.00k
Timestamp 10/11/2022 11:42:16 PM
Step Count 160 Switch Count 2
Page Faults 0
Page Reclaims 88
Page Swaps 0
Voluntary Context Switches 10
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 264
85 data fifth_drthtest_drt;
86 set sashelp.class;
87 run;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.FIFTH_DRTHTEST_DRT has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 684.84k
OS Memory 25508.00k
Timestamp 10/11/2022 11:42:16 PM
Step Count 161 Switch Count 2
Page Faults 0
Page Reclaims 88
Page Swaps 0
Voluntary Context Switches 10
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 264
88
89
90 *create a macro variable with the new and old names;
91 proc sql NOPRINT;
92 select catx("=", /*separator between name and new name*/
93 memname, /*original name*/
94 substr(memname, 1, length(memname)-4) /*new name, 4 is length of suffix to remove*/
95 ) into :rename_list separated by " "
96 from sashelp.vtable
97 where libname='WORK' and memname like '%_DRT';
98 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
user cpu time 0.01 seconds
system cpu time 0.01 seconds
memory 5266.28k
OS Memory 30372.00k
Timestamp 10/11/2022 11:42:16 PM
Step Count 162 Switch Count 0
Page Faults 0
Page Reclaims 44
Page Swaps 0
Voluntary Context Switches 0
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 0
99
100 *display macro variable for confirmation;
101 %put &rename_list.;
SYMBOLGEN: Macro variable RENAME_LIST resolves to FIFTH_DRTHTEST_DRT=FIFTH_DRTHTEST FIRST_DRT=FIRST FOURTH_MULTI_DRT=FOURTH_MULTI
SECOND_DRT=SECOND THIRD_DRT=THIRD
FIFTH_DRTHTEST_DRT=FIFTH_DRTHTEST FIRST_DRT=FIRST FOURTH_MULTI_DRT=FOURTH_MULTI SECOND_DRT=SECOND THIRD_DRT=THIRD
102
103 options symbolgen;
104 proc datasets lib=work nodetails nolist;
SYMBOLGEN: Macro variable RENAME_LIST resolves to FIFTH_DRTHTEST_DRT=FIFTH_DRTHTEST FIRST_DRT=FIRST FOURTH_MULTI_DRT=FOURTH_MULTI
SECOND_DRT=SECOND THIRD_DRT=THIRD
105 change &rename_list;
106 quit;
NOTE: Changing the name WORK.FIFTH_DRTHTEST_DRT to WORK.FIFTH_DRTHTEST (memtype=DATA).
NOTE: Changing the name WORK.FIRST_DRT to WORK.FIRST (memtype=DATA).
NOTE: Changing the name WORK.FOURTH_MULTI_DRT to WORK.FOURTH_MULTI (memtype=DATA).
NOTE: Changing the name WORK.SECOND_DRT to WORK.SECOND (memtype=DATA).
NOTE: Changing the name WORK.THIRD_DRT to WORK.THIRD (memtype=DATA).
NOTE: PROCEDURE DATASETS used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 364.06k
OS Memory 25508.00k
Timestamp 10/11/2022 11:42:16 PM
Step Count 163 Switch Count 20
Page Faults 0
Page Reclaims 48
Page Swaps 0
Voluntary Context Switches 90
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 8
107 options nosymbolgen;
108
109
110 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
120
User: fkhurshed
@Zeus_Olympus wrote:
Dear all,
I have a library named TEST where all datasets names have a common suffix _DRT i.e.
First_DRT
Second_DRT
Third_DRT
etc...
I need to rename all of the datasets to
First
Second
Third
etc.
without the common suffix
Any advice would be more than welcome
Thanking you in advance
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.