BookmarkSubscribeRSS Feed
heffo
Pyrite | Level 9

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. 

4 REPLIES 4
Tom
Super User Tom
Super User

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;
    ...

 

 

heffo
Pyrite | Level 9

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... 

Tom
Super User Tom
Super User

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.

Tom
Super User Tom
Super User
Show how the table (or at least the fields you are pulling) is defined in the database.
Is it one field? Multiple fields? Are they VARCHAR? CLOB? Something Else?
What options does the language of the database provide for splitting the values into pieces? Does SUBSTR() or SUBSTRING() work on those data types?

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1807 views
  • 0 likes
  • 2 in conversation