Published on :
Macro, ETL EXTERNAL

CSV File Import Macro

This code is also available in: Deutsch Español Français
Attention : This code requires administrator privileges.
This SAS© macro, `%csvimport`, is designed to facilitate the import of data files in CSV (Comma Separated Values) format into a SAS© dataset. It is configurable and accepts the CSV file name (without the extension), an output SAS© dataset name, and the directory where the CSV file is stored. Using `PROC IMPORT` with the `dbms=csv` option ensures correct import of comma-delimited data, and the `replace` option allows overwriting an existing dataset with the same name. The macro also includes a `%put` statement to display log messages during its execution.
Data Analysis

Type : EXTERNAL


The `%csvimport` macro is explicitly designed to read data from external CSV files, whose path is dynamically constructed from the `dir` and `dataset` parameters. Additionally, the reference file `Market_Return_Prior_10Days.sas` also uses `infile` statements to read data from external CSV files (`IPOInformation_1221.csv`, `Market_Index_Return.csv`), confirming the use of external data sources other than SASHELP and non-datalines.

1 Code Block
MACRO DEFINITION
Explanation :
This block defines the `%csvimport` macro. It is called with three arguments: `dataset` (the base name of the CSV file), `outds` (the name of the output SAS dataset, by default the same as `dataset`), and `dir` (the path to the directory containing the CSV file, by default '../data'). Inside the macro, a `%put` statement displays a message in the log to indicate the file being loaded. Then, `PROC IMPORT` is used to read the CSV file specified by `datafile="&dir./&dataset..csv"`. The `out=&outds` parameter names the created SAS dataset, `dbms=csv` indicates that the source file is a CSV, and `replace` allows replacing an existing SAS dataset with the same name.
Copied!
1%macro csvimport(dataset, outds=&dataset, dir=../DATA);
2 
3 %put Loading in &dataset from &dir. ;
4
5 PROC IMPORT datafile="&dir./&dataset..csv"
6 out=&outds
7 dbms=csv
8 replace;
9 RUN;
10%mend csvimport;
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.
Copyright Info : Copyright © 2022, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. (detected in file `print_macro_parameters.sas`)