BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
FLINT
Calcite | Level 5
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.
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

 

View solution in original post

8 REPLIES 8
SuryaKiran
Meteorite | Level 14

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;

 

Thanks,
Suryakiran
FLINT
Calcite | Level 5
Surya ,
I am the same code and the data set is like
Column Ais Bank_code
B is PAN_Number
C is party_name
Here The problem is that some of the pan numbers are missing where where blank space is getting populated instead of null
SuryaKiran
Meteorite | Level 14

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;
Thanks,
Suryakiran
Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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

 

FLINT
Calcite | Level 5
Bingo..it worked wonders....thanks
Reeza
Super User

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.

 

FLINT
Calcite | Level 5
@Reeza I m not using export cause I have to use macro variable in the header of the CSV file

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 8 replies
  • 3808 views
  • 1 like
  • 5 in conversation