I have some data in an SQL server database, some of the data is longer than 32767 characters. I would like to save this data to individual files.
I know how to do it in SAS when the data is there
*Write the data to file. Then remove the variable that contains the data but keep the variable that contains the path to the file.;
data _null_;
set work.fileAttachement;
length filePathName $500.;
*Set the file path!;
filePathName = cats("C:\temp\",Filename);
file dummy filevar=filePathName;
*Convert the data from Base64 to binary. NB! This does not work when the file is larger than 32 767 chars... :-(;
body = input(body,$BASE64X32767.);
put body; *Print the actual content to the file.;
run;
The problem is that most of the files are too long. So, the question is, how can I write the Base64 data directly to file from the SQL step where I extract the data (using proc sql and "connect to odbc as ...". BTW, one of the variables in the data set contains the actual file name that I want to use.
Why do you think you need to use PROC SQL?
Just make a libref pointing to your source database system.
libname mydb odbc ..... ;
data _null_;
set mydb.fileAttachement;
...
I did try that way, after you told me, but it only reads 32 767 chars. 😞
I found some code that can split a large string into substrings (around 900!) but unfortunately, that does not seem to be working. I suspect that it might be because it is saved in the wrong format (if i get smaller files and use the format $BASE64X32767. it works). So I'm still on page one on this one...
Sounds like the problem is that the fields in your database are longer than SAS allows for character fields.
So in that case you need to use pass through SQL (or have your DB admin make you a view) that can parse the long fields into multiple shorter fields.
But even then you could create a ViEW using PROC SQL and then just normal data step/proc step to process the view.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.