-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete_Rename_Add_Label_Format.sas
97 lines (87 loc) · 2.89 KB
/
Delete_Rename_Add_Label_Format.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*#####################################################################################*/
/* DELETE_DATASET */
/*
%delete_dataset(data_1 data_2 );
%delete_dataset( TEMP_: );
*/
%macro delete_dataset(list);
%local list;
proc datasets lib=work memtype=data nolist ;
delete &list;
quit;
run;
%mend;
/*#####################################################################################*/
/* RENAME_VAR */
/*
data something;
set sashelp.cars;
run;
%rename_var(something , old_name=Make , new_name = Make_of_car );
If the dataset doesn't exist then ABORT.
If warning is YES and the old_name doesn't exist then ERROR ABORT
ELSE If warning is YES and the new_name exists then ERROR ABORT
ELSE If warning is NO and the old_name doesn't exist then NOTE
ELSE If warning is NO and the new_name exists then NOTE
ELSE (old_name exists and new_name doesn't) RENAME
*/
%macro rename_var(ds , old_name= , new_name= , warn=YES);
%if %dataset_exist(&ds)=0 %then %do;
%put ERROR: The dataset passed to the rename macro does not exist: &ds ;
%abort;
%end;
%if &warn = YES and %var_exist(&ds , &new_name) %then %do;
%put ERROR: The variable &new_name already exists in the dataset &ds;
%abort;
%end;
%else %if &warn = YES and %var_exist(&ds , &old_name)=0 %then %do;
%put ERROR: The variable &old_name does not exist in the dataset &ds;
%abort;
%end;
%else %if &warn = NO and %var_exist(&ds , &new_name) %then %do;
%put NOTE: The variable &new_name already exists in the dataset &ds so nothing will be renamed;
%end;
%else %if &warn = NO and %var_exist(&ds , &old_name)=0 %then %do;
%put NOTE: The variable &old_name does not exist in the dataset &ds so nothing will be renamed;
%end;
%else %do;
PROC DATASETS LIBRARY=work nolist;
MODIFY &ds ;
RENAME &old_name=&new_name;
QUIT;
RUN;
%end;
%mend;
/*#####################################################################################*/
/* ADD_LABEL */
/*
data names_of_people;
input name $;
datalines;
Joe
Tim
Brendan
;
run;
%add_label( names_of_people , name , "Name of person" );
%add_label( names_of_people , hat , "Name of person" );
%add_label( names_of_people , hat , "Name of person" , warn=NO );
%print_vars(names_of_people);
*/
%macro add_label(ds_label , var_label , label , warn=YES);
%if &warn = YES | %Var_Exist(&ds_label , &var_label ) %then %do;
PROC DATASETS LIBRARY=work nolist;
MODIFY &ds_label ;
label &var_label = &label;
RUN;
quit;
%end;
%mend;
/*#####################################################################################*/
/* DROP_FORMAT */
%macro drop_format(ds , var);
proc datasets lib=work memtype=data nolist;
modify &ds;
attrib &var format=;
run;
%mend;