BookmarkSubscribeRSS Feed
Olscream
Fluorite | Level 6

Hello everyone,

 

I would like to export to the CSV format a SAS dataset with a large number of columns (~2200) with a total length of around 42000 characters when all column titles concatened. The output final CSV file doesn't exceed 400MB because of a low number of rows.

 

But when I open my CSV file, I see that more than 300 column names are missing. How is that possible ? Would you know how to fix it please?

 

More details :

-I've used a proc export to export as CSV.

-When I use a proc export to output a XLSX file, I don't get this problem.

-It seems that the problem is the total length of the concatened column names since it works well for the other rows which are shorter (when all concatened).

-It is like I need to set a LRECL option to a higher limit, but it doesn't exist in a proc export.

 

Thank you very much in advance for your help.

 

Best regards

7 REPLIES 7
Reeza
Super User
Show your code.
Have you tried a data step and explicitly setting the record length?
How are you checking the CSV, via a text editor or Excel? FYI - don't use Excel it can sometimes not parse records correctly causing issues.

Olscream
Fluorite | Level 6

I do a simple export as following :

proc export data=My_Table
dbms=csv
outfile="&month_My_Table.csv"
replace;
run;

&month is a number like 201501.

I've noticed the error thanks Microsoft Excel 2016 but also in another programming language.

Kurt_Bremser
Super User

The code created by proc export tries to write the header as one string, which exceeds the maximum length of character values (32767).

Locate the header in the code, and split it to two separate put statements with a line hold (@) in the first.

Olscream
Fluorite | Level 6
Could you show a snippet of code in order to do that please ?
Reeza
Super User
*Create demo data;
data class;
	set sashelp.class;
	label age='Age, Years' weight = 'Weight(lbs)' height='Height, inches';
run;

proc sql noprint;
create table temp as
select name as _name_, label as _label_
from dictionary.columns
where libname="WORK" and upcase(memname)="CLASS";
select cats(quote(name),"n") into :varList separated by ' '
from dictionary.columns
where libname="WORK" and upcase(memname)="CLASS";
quit;

data _null_;
file "&sasforum.\datasets\TwoLinesHeader.csv" dsd lrecl = 40000;
set class;
if _n_ = 1 then do;
do until(eof);
    set temp end=eof;
    put _name_ @;
    end;
put;

put (&varList) (:);
run;

Modified version of this which prints names and labels. I removed the labels section. 

https://gist.github.com/statgeek/ae153e40af0d35dce02d1c8102ff3b94

Olscream
Fluorite | Level 6

Thanks for your answer. However it could work but I got several errors when trying it, the first one being :

 

proc sql noprint;
select cats(quote(name),"n")into : varList separated by ' ' from dictionary.columns where LIBNAME = upcase("WORK") and MEMNAME = upcase("My_Table"); quit; %put &varList.;
Spoiler
ERROR: The length of the value of the macro variable VARLIST (65540) exceeds the maximum length (65534). The value has been
truncated to 65534 characters.

But on the other hand, it works when removing the CATS and QUOTE functions such as :

proc sql noprint;
select name into : varList separated by ' '
from dictionary.columns
where LIBNAME = upcase("WORK")
and MEMNAME = upcase("My_Table");
quit;
%put &varList.;
Spoiler
%put &varList.;
Col1 Col2 Col3 ...

Would you know how to "overpass" this limit please ?

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 3244 views
  • 4 likes
  • 3 in conversation