DATA Step, Macro, Functions and more

Importing invalid SAS dataset names and exporting as .dta files

New Contributor
Posts: 2

Importing invalid SAS dataset names and exporting as .dta files

[ Edited ]



I am trying to import 79 different SAS datasets and export them as .dta files. Part of the issue I am having is that the .sas7bdat file names are not valid SAS dataset names--they contain only numeric characters.


I tried to write code that would at least import one dataset and save it out as a .dta and then create a loop to go through the rest of the SAS datasets, but I cannot even get past the first step.



%let folder = C:\Example folder;

proc printto log="&folder.\SAS files\Logs\01 Convert SAS files to dta.log" new; run; libname sasinput "&folder.\Natives"; options validvarname=any; proc export data=sasinput.01095552 outfile= "&folder.\SAS files\Output\01095552.dta" REPLACE; run; proc printto; run;

It seems from researching that I need to specify the system option validvarname=any but it does not appear that I am specifying it correctly.



I am looking to loop through SAS datasets sequentially numbered beginning with 01095552.sas7bdat and ending with 01095630.sas7bdat. I am having a lot of trouble figuring out what I am doing wrong.


Thank you for any help. If you could not tell I am fairly inexperienced with SAS, although I have a lot of STATA experience.


Super User
Posts: 8,214

Re: Importing invalid SAS dataset names and exporting as .dta files

Posted in reply to mjcochran

Two questions: 1. You use the macro variable &folder, but don't show the code that created that macro variable and

2. validvarname=any doesn't apply to SAS dataset names. They CAN'T begin with a number or include any special characters (other than an underscore).


I would guess that the files were either renamed, or created by a program outside of SAS.


My suggestion would be to write a program that renames the files, keeping them as is, but beginning with an underscore. Then you should be able to export them.


Art, CEO,


New Contributor
Posts: 2

Re: Importing invalid SAS dataset names and exporting as .dta files

Hi Art,


Yes, forgot to copy the line of code that generates the macro variable &folder in my post above (fixed now).


Renaming the files makes my code work. Would've liked to have avoided it, but thankfully isn't a big deal in the end. Thanks for the help! 

Ask a Question
Discussion stats
  • 2 replies
  • 2 in conversation