BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jeg123
Calcite | Level 5

Im trying to create a macro that has two arguments and then reads a CSV. Can somebody help me what I'm doing wrong?

 

%macro read_csv(csv_location, csv_file);

%let file_path = cats(&csv_location, &csv_file);
proc import datafile= &file_path;
out=test
dbms=csv
replace;

run;
%mend read_csv;

 

%read_csv("/file_folder/", "csv_name.csv");

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

First, since cats is a function that can only be used within the data step, change it as follows

data _null_;
  call symputx('file_path',cats(&csv_location, &csv_file));
run;

Then, the semicolon after proc import datafile= is unnecessary.
Also, double quotation marks are necessary because we are concatenating the filepath string in the data step.

proc import datafile= "&file_path"

 

Finally, let's first create the code that works correctly without macro variables before starting macro coding.

The following options are useful for debugging macros Please refer to them.

options mprint mlogic symbolgen;

 

View solution in original post

3 REPLIES 3
japelin
Rhodochrosite | Level 12

First, since cats is a function that can only be used within the data step, change it as follows

data _null_;
  call symputx('file_path',cats(&csv_location, &csv_file));
run;

Then, the semicolon after proc import datafile= is unnecessary.
Also, double quotation marks are necessary because we are concatenating the filepath string in the data step.

proc import datafile= "&file_path"

 

Finally, let's first create the code that works correctly without macro variables before starting macro coding.

The following options are useful for debugging macros Please refer to them.

options mprint mlogic symbolgen;

 

Jeg123
Calcite | Level 5
Thanks for the explanation, worked great, thanks!
ballardw
Super User

Strongly suggest if you are going to be importing files this way that you add a GUESSINGROWS statement with either Max or large value like 30000. Otherwise proc import only uses a few rows to set variable properties like type, length, informat and format. So if you have a variable not always used you may end up with a one character field because the first few rows had missing values. Or if a column has a mix of all digit values and letter +digits, like part numbers, if the first rows contain all digits the imported variable can be numeric and all of the values with letters will be 'invalid data' and have missing values.

Or if you have a text field such as comment the length may be set shorter than needed.

 

Last, if many of these CSV are supposed to be of the same structure and values you are way better off writing a data step to ensure that all of the variable properties are set the same. Otherwise you will likely have to "fix" data, on a set-by-set basis when you go to combine data for analysis or reporting.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 517 views
  • 1 like
  • 3 in conversation