-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamic_file_naming.sas
executable file
·89 lines (63 loc) · 2.1 KB
/
dynamic_file_naming.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
/****************************************************************
Dynamically naming your files with the current date
****************************************************************/
/************************************************************
Step 1: Creating the macro variable with today's date
************************************************************/
/**************************
Using the %LET statement
**************************/
%let current_date = %sysfunc(today(), yymm.);
/* View macro variable value */
%put &=current_date;
/**************************
Using the DATA step
**************************/
data _null_;
todays_date = put(today(), yymm.);
/* Create macro variable */
call symputx('current_date', todays_date);
run;
/* View macro variable value */
%put &=current_date;
/**************************
Using Python
**************************/
proc python;
submit;
## Get the date
from datetime import date
today = date.today().strftime("%Y-%m-%d")
## Create a SAS macro variable
SAS.symput('current_date_python', today)
endsubmit;
quit;
/* View macro variable value */
%put &=current_date;
/************************************************************
Step 2: Use the macro variable when creating a table or file
************************************************************/
/****************************************
a. Create a SAS table with the current date
****************************************/
data work.toyota_¤t_date;
set sashelp.cars;
MPG_Avg = mean(MPG_City, MPG_Highway);
where Make = 'Toyota';
run;
/*********************************************
b. Create an Excel file with the current date
*********************************************/
/* Specify where you want to create the file */
%let outpath = %SYSGET(HOME);
/* Create the Excel file */
ods excel file = "&outpath./Toyota_Report_¤t_date..xlsx";
/* Add text to Excel */
proc odstext;
p "List of cars as of ¤t_date" / style = [fontsize=18pt] ;
run;
/* Print a list of cars */
proc print data=work.toyota_¤t_date noobs;
var Make Model MSRP Invoice MPG_Avg;
run;
ods excel close;