Hi, I ran the following code and the symbol "-" is not recognized. I cannot change it since I am importing more than 100 csv files with the symbol "-" in most of the file names. Is there a way for SAS to recognize the symbol?
data TEST.ABC-POST;
%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
infile "&DATA_PATH.CSV files/ABC-POST.csv" delimiter=','
...
run;
This is the log:
207
208
209 *ABC-POST variables;
210 data TEST.ABC-POST;
_
22
200
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, /, ;, _DATA_, _LAST_, _NULL_.
ERROR 200-322: The symbol is not recognized and will be ignored.
A CSV file is just a file. It can have any name it wants. But a SAS dataset's name has to follow SAS's rules.
So just generate a valid SAS name to use as the dataset member name.
data TEST.ABC_POST;
infile "&DATA_PATH.CSV files/ABC-POST.csv" dsd firstobs=2 truncover;
length var1 8 var_next $32 .... var_last 8;
input var1 -- var_last;
run;
If you can't find one just use a number.
data have;
infile cards truncover;
length row 8 memname $32 filename $256 ;
row+1;
input filename $256.;
memname=scan(filename,-2,'./\');
if not nvalid(memname,'v7') then memname=cats('csv',row);
cards;
&DATA_PATH.CSV files/ABC-POST.csv
normal.csv
this_name_is_going_to_be_way_too_long_to_use_as_dataset_name.csv
;
A CSV file is just a file. It can have any name it wants. But a SAS dataset's name has to follow SAS's rules.
So just generate a valid SAS name to use as the dataset member name.
data TEST.ABC_POST;
infile "&DATA_PATH.CSV files/ABC-POST.csv" dsd firstobs=2 truncover;
length var1 8 var_next $32 .... var_last 8;
input var1 -- var_last;
run;
If you can't find one just use a number.
data have;
infile cards truncover;
length row 8 memname $32 filename $256 ;
row+1;
input filename $256.;
memname=scan(filename,-2,'./\');
if not nvalid(memname,'v7') then memname=cats('csv',row);
cards;
&DATA_PATH.CSV files/ABC-POST.csv
normal.csv
this_name_is_going_to_be_way_too_long_to_use_as_dataset_name.csv
;
Ok, so manually change in the SAS datasets' names the "-" to "_". I didn't understand your second option. What do you mean if I cannot find one just use a number? Sorry, I'm new to programming.
@ANKH1 wrote:
Ok, so manually change in the SAS datasets' names the "-" to "_". I didn't understand your second option. What do you mean if I cannot find one just use a number? Sorry, I'm new to programming.
You mentioned you wanted to convert a LOT of files. So rather than generating all of that code manually you could use a PROGRAM to read the list of files and generate the code. So how is the PROGRAM going to know what is a valid SAS name to use for any given CSV filename? One way is to not even try, instead just number the files and use the number to generate the names. So the first file could be used to create a dataset named CSV1 and the second file to create a dataset named CSV2. You could actually use the original filename as the label or part of the label for the dataset to help you find the right dataset later.
data csv1(label="Strange file - name.csv");
infile "Strange file - name.csv" dsd firstobs=2 truncover;
...
run;
data csv2(label="Another strange file - name.csv");
infile "Another strange file - name.csv" dsd firstobs=2 truncover;
...
run;
Here is an example of logic I used in the %csv2ds() macro to make sure each name is valid.
See if you can figure out from reading the help for the functions used what it is doing.
* Replace adjacent non-valid characters with single underscore ;
name=translate(trim(prxchange('s/([^a-zA-Z0-9]+)/ /',-1,name)),' _','_ ');
name=prxchange('s/(^[0-9])/_$1/',1,name);
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.