Export SAS Datasets as a .CSV File with a dash in the input file name

Reply
Occasional Contributor
Posts: 17

Export SAS Datasets as a .CSV File with a dash in the input file name

Hello,

 

I'm following the steps in How to Export SAS Datasets as a .CSV File in SAS University Edition to export a CSV file from a SAS dataset, with something like this:

 

libname example "/folders/myfolders";
run;

proc export data=example.Example-Data
  outfile='/folders/myfolders/ExampleCSV.csv'
  dbms=csv
  replace;
run;

However, my input file has a dash in it e.g. the file is called `Example-Data.sas7bdat`.

 

SAS give me an error because "-" (dash) is not allowed in variable names. I'm not able to rename the file, so I need a way to tell SAS to get the file name from a string, instead of a variable name.

 

How can I do that?

Super User
Posts: 10,280

Re: Export SAS Datasets as a .CSV File with a dash in the input file name

Dashes are not allowed in valid SAS names. You could work around by setting

options validvarname=any;

and

data=example.'Example-Data'n

but I strongly, and I mean STRONGLY, recommend to rename the .sas7bdat file from the commandline and to work with standard SAS names.

How was the dataset created in the first place? Fix the mistake there.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 17

Re: Export SAS Datasets as a .CSV File with a dash in the input file name

Posted in reply to KurtBremser

Thank you Kurt! I'll give that a try.

 

I receive CSV files from an external party / system that exists for many years (running Windows) and the files come with all sorts of file names that I cannot control (nor it is feasible to change such an old system), so I need to read them however they are named.

 

 

Super User
Posts: 10,280

Re: Export SAS Datasets as a .CSV File with a dash in the input file name

External files (like csv's) can have any name that the OS supports. SAS datasets need to adhere to valid SAS names. If you always receive data in csv format, you set the dataset names when importing.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 23,773

Re: Export SAS Datasets as a .CSV File with a dash in the input file name

The name of the SAS data set has nothing to do with the name of the original csv files. I understand not changing processes, but this is known as technical debt and you need to start somewhere. At least moving forward, make sure you don't continue this practice.  

 


@the_yeti wrote:

Thank you Kurt! I'll give that a try.

 

I receive CSV files from an external party / system that exists for many years (running Windows) and the files come with all sorts of file names that I cannot control (nor it is feasible to change such an old system), so I need to read them however they are named.

 

 


 

Super User
Posts: 10,787

Re: Export SAS Datasets as a .CSV File with a dash in the input file name

filename with a dash is illegal name in sas.

1) rename it as a legal name.

2)

options  validmemname=extend ;

 

proc export data=example.'Example-Data'n

 

Ask a Question
Discussion stats
  • 5 replies
  • 229 views
  • 4 likes
  • 4 in conversation