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?
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.
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.
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.
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.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.