DATA Step, Macro, Functions and more

Reading a .csv file into SAS using PROC IMPORT errors out not a valid name

Reply
New Contributor
Posts: 2

Reading a .csv file into SAS using PROC IMPORT errors out not a valid name

I'm a newbie with SAS. I'm trying to convert a CSV file into SAS format. Please find the code and logs below and let me know what is wrong.

 

 

OPTIONS NOFMTERR ;
%let table=%sysget(v1);
%let outfil=%sysget(v2);
%macro importa ;
proc import datafile="&table" out="&outfil" dbms=csv replace ; run ;
%mend importa ;
%importa ;

 

 

CSV file:

 

27697,1,PF,2,33,1,5,21,1987-11-09,0,1983-06-28,1987-11-09,,29,1,11,0,4,0,,32,,0,2,,,0,,I,2016-11-30,23950,4,28

 

 

 

When I run the sas command like:

 

 

sas -noterminal -set v1 /mypath/temp.csv -set v2 /mypath /mypath/import.sas

 

 

it errors out that:

 

 

 

 

NOTE: SAS initialization used:
real time 0.02 seconds
cpu time 0.01 seconds
1 OPTIONS NOFMTERR ;
2
3 %let table=%sysget(v1);
4 %let outfil=%sysget(v2);
5
6 %macro importa ;
7 proc import datafile="&table" out="&outfil" dbms=csv replace ; run ;
8 %mend importa ;
9 %importa ;
ERROR: ""/mypath"" is not a valid name.
NOTE: PROCEDURE IMPORT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
NOTE: The SAS System stopped processing this step because of errors.
10

 

 

Super User
Posts: 10,526

Re: Reading a .csv file into SAS using PROC IMPORT errors out not a valid name

It appears that

set v2 /mypath /mypath/import.sas

 

is getting the value of /mypath for V2 because of the space

 

V2 would have to resolve to a valid dataset name or LIbrary.dataset name. So no characters other than letters, digits and underscore and cannot start with a digit

 

set v2 mydatasetname perhaps

 

New Contributor
Posts: 2

Re: Reading a .csv file into SAS using PROC IMPORT errors out not a valid name

Thanks for your response. I tried both of these but I still get same error:

 

sas -noterminal -set v1 /mypath/temp.csv -set v2 test_csv.dataset /mypath/import.sas

sas -noterminal -set v1 /mypath/temp.csv -set v2 test_csv /mypath/import.sas

Both returns the same error:

 

ERROR: ""test_csv"" is not a valid name.

ERROR: ""test_csv.dataset"" is not a valid name.
Super User
Posts: 10,526

Re: Reading a .csv file into SAS using PROC IMPORT errors out not a valid name

 

Where you use this line the OUT= should be a dataset or library.setname and would not use quotes. I didn't look further than the / the first time.

 

proc import datafile="&table" out="&outfil" dbms=csv replace ; run ;

try

 

proc import datafile="&table" out=&outfil dbms=csv replace ; run ;

but any library referenced needs to exist before the proc import step.

 

 

If v2 is supposed to be a data set name then test_csv.dataset   would be attempting to create/use a dataset with the NAME of dataset in a library named test_csv.

 

I haven't done any unix(?) command syntax in over 20 years so if there is an issue with the syntax there I can't help.

Super User
Super User
Posts: 6,502

Re: Reading a .csv file into SAS using PROC IMPORT errors out not a valid name

You shouldn't be trying to read or write anything using the root node of your Unix file system, but perhaps that is just a simplied example for posting the question?

 

I also noticed that your sample CSV file does NOT have a line with the column names. If that is true then make sure to tell PROC IMPORT that it needs to generates names by itself.

 

Also make sure that the name of your physical file is in all lowercase letters.  The directory name part can contain mixed upper and lowercase letters but the membername part of the filename must be all lowercase.  For example you could use "~/MySASData/example1" as the name and SAS would create the dataset EXAMPLE1 using the physical file name of 'example1.sas7bdat' in the 'MySASData' directory under your home directory.

 

If those don't fix the issue then I think that most likely your problem is that PROC IMPORT is not flexible enough to use a quoted physical name as the name for the output dataset.  Perhaps if you just use this simple trick you can get it to work?

 

OPTIONS NOFMTERR ;
%let table=%sysget(v1);
%let outfil=%sysget(v2);

* Make an empty dataset to create the SYSLAST automatic variable;
data "&outfil"; run;

proc import
  out=&syslast replace
  datafile="&table" dbms=csv 
;
  getnames=no;
run;
Ask a Question
Discussion stats
  • 4 replies
  • 114 views
  • 0 likes
  • 3 in conversation