That is what the DSD option on the FILE statement if for.
data have ;
input a b c ;
cards;
1 2 3
4 . 6
. . 7
;
data _null_;
set have;
file log dsd dlm=',';
put a b c;
run;
Results
1,2,3 4,,6 ,,7
Please provider some sample data and the full code your using or use sample SAS datasets as below. It's something to do with your data or code you have.
Filename out_CSV "\Output\out_file.csv";
Data _null_;
set sashelp.class;
File out_CSV DLM=',';
PUT Name Age Sex Height Weight;
run;
Instead of writing individual variables, create a string with like delimited and write that string to file.
data have;
set sashelp.class;
If sex="M" then call missing(Sex);
run;
Filename out_CSV "\Output\out_file.csv";
Data _NULL_;
set Have;
File out_CSV DLM=',';
out_Line=CATS(Name,",",Sex,",",Age,",",Height,",",Weight);
PUT out_Line;
run;
Get rid of the delimiter, and code it yourself:
Data _null_;
set dataset;
File "filename" noprint;
if _n_ eq 1 then do
****
****
Put 'A,B,C';
End;
PUt A +(-1) ',' B +(-1) ',' C;
run;
However, this method runs into trouble when A or B has a format associated with it.
That is what the DSD option on the FILE statement if for.
data have ;
input a b c ;
cards;
1 2 3
4 . 6
. . 7
;
data _null_;
set have;
file log dsd dlm=',';
put a b c;
run;
Results
1,2,3 4,,6 ,,7
If there's nothing particularly special about your export, any reason to not use PROC EXPORT?
proc export data=sashelp.class outfile=filename dbms=csv replace;run;
@FLINT wrote:
I am creating CSV file from sas dataset using following code
Data _null_;
set dataset;
File "filename" DLM=',';
if _n_ eq 1 then do
****
****
Put 'A,B,C';
End;
PUt A B C;
run;
Problem is that a blank space is introduced in fileds with null values.
Please help how to avoid that.
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: