Published on :

Folder Creation and Data Copy

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
The script includes a macro named `create_newfolder` which takes a folder path as a parameter. This macro checks for the existence of the folder; if it does not exist, it determines the operating system (Windows or Linux) via `&sysscp` and executes the appropriate system command (`md` or `mkdir -p`) to recursively create the folder. After the macro definition, the script creates a folder `D:\SAS©\mactest`, assigns a SAS© library named `mylib` to it, and then copies the `CLASS` dataset from the `SASHELP` library to `mylib.class`.
Data Analysis

Type : SASHELP


The script uses the `CLASS` dataset from the internal `SASHELP` library.

1 Code Block
MACRO DEFINITION
Explanation :
This block defines the `create_newfolder` macro. It takes one argument `newfld` (the path to the folder to create). It uses `%sysfunc(fileexist())` to check if the folder already exists. If not, it uses `%sysexec` with `md` (for Windows) or `mkdir -p` (for Linux) to create the folder. The use of `%sysexec` is an administrative function allowing system commands execution and requires appropriate privileges.
Copied!
1%macro create_newfolder(newfld);
2
3%*---------------------------------------------------------;
4%*check for the existence of the folder;
5%*---------------------------------------------------------;
6
7%IF %sysfunc(fileexist(&newfld)) %THEN %put NOTE:The directory "&newfld" already EXISTS.;
8
9%*---------------------------------------------------------;
10%*create the folder(s) recursively IF absent;
11%*---------------------------------------------------------;
12%ELSE %DO;
13 %*---------------------------------------------------------;
14 %*check the operating system and use the OS specific command;
15 %*---------------------------------------------------------;
16 %IF "%bquote(%substr(&sysscp,1,3))"="WIN" %THEN %sysexec md "&newfld";
17 %ELSE %IF "%bquote(%substr(&sysscp,1,3))"="LIN" %THEN %sysexec mkdir -p "&newfld";
18
19 %put NOTE:The directory "&newfld" has been CREATED.;
20%END;
21
22
23%mend csg_create_newfolder_001;
2 Code Block
MACRO CALL / LIBNAME
Explanation :
This block calls the `create_newfolder` macro to create the `D:\SAS\mactest` folder. Then, it assigns the `mylib` library name to this new folder, making it accessible for SAS operations.
Copied!
1%create_newfolder(D:\SAS\mactest);
2 
3LIBNAME mylib "D:\SAS\mactest";
4 
3 Code Block
DATA STEP Data
Explanation :
This DATA STEP block copies the `CLASS` dataset from the SASHELP library (implicitly, as `set class;` without a qualified libname refers to `SASHELP.CLASS` if `WORK.CLASS` does not exist) to the new `mylib` library, thereby creating `mylib.class` in the `D:\SAS\mactest` folder.
Copied!
1DATA mylib.class;
2 SET class;
3RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.