BookmarkSubscribeRSS Feed
santhosh
Fluorite | Level 6
I need to create .csv output should have space in rows
Sample output
No particular amount
1 I BOOKS 5000
2 A PLANE 3000
3 B DOUBLE ROOL 2000
4 II STATENAORY 1000
5 A PEN 500
6 B PENCIL 500
5 REPLIES 5
MayurJadhav
Quartz | Level 8

You can do this using option:

delimiter=" ";

for example:

data credit;
set sashelp.credit(obs=10);
run;
proc print;

proc export data=credit
    outfile="/home/u61950255/Files/data.csv"
    dbms=csv
    replace;
    delimiter="	";
    putnames=NO;
run;

But beware that it would cause problem when you have space in between the data in the same column. @santhosh 

Look at the data from csv file generated by above code with delimiter=" ";

MayurJadhav_0-1683886182222.png

 

 

 

Mayur Jadhav
BI Developer. Writer. Creative Educator.

SAS Blog → https://learnsascode.com
YouTube Channel: → https://www.youtube.com/@imayurj
Tom
Super User Tom
Super User

Your desired output does not look like something usable.  You appear to have three variables named No, particular, and amount.  But some of your data rows appear to have more than 3 values on them.

 

Normally in a delimited text file (like a CSV file) you have to quote values that contain the delimiter.

So, depending on which value the space belongs to, your file might look like this:

No particular amount
1 "I BOOKS" 5000
2 "A PLANE" 3000
3 "B DOUBLE ROOL" 2000
4 "II STATENAORY" 1000
5 "A PEN" 500
6 "B PENCIL" 500

SAS can generate that type of file using a simple data step with the DSD option of the FILE statement.

data _null_;
  set have;
  file 'mytext_file.txt' dsd dlm=' ';
  if _n_=1 then put 'No particular amount';
  put no particular amount;
run;

 

santhosh
Fluorite | Level 6
Dear Sir,
There is leading space for some rows in second column
2,3 rows there should be a space before letter A and B

No particular amount
1 "I BOOKS" 5000
2 " A PLANE" 3000
3 " B DOUBLE ROOL" 2000
4 "II STATENAORY" 1000
5 " A PEN" 500
6 " B PENCIL" 500
Patrick
Opal | Level 21

Your question is a bit underspecified so my answer based on some assumptions.

data work.have;
  infile datalines dsd dlm=' ';
  input no particular:$40. amount;
  datalines;
1 "I BOOKS" 5000
2 " A PLANE" 3000
3 " B DOUBLE ROOL" 2000
4 "II STATENAORY" 1000
5 " A PEN" 500
6 " B PENCIL" 500
;
proc export 
  data=work.have
  outfile= "c:\temp\want.csv"
  dbms=csv
  ;
quit;

c:\temp\want.csv opened with a text editor

Patrick_0-1683970507138.png

 

Tom
Super User Tom
Super User

Leading spaces do not translate well into a delimited file.  

To preserve them when reading a file like you could try using the $CHAR informat instead of the normal $ informat.

data have;
  infile cards dsd dlm=' ' truncover;
  input No particular :$char20. amount;
cards;
1 "I BOOKS" 5000
2 " A PLANE" 3000
3 " B DOUBLE ROOL" 2000
4 "II STATENAORY" 1000
5 " A PEN" 500
6 " B PENCIL" 500
;

But to write that back out you will need to NOT use the DSD option on the FILE statement.

Which means you cannot use PROC EXPORT.

 

So instead just use the $QUOTE. format to add quotes around every string variable, just in case it has any embedded space.

data _null_;
 file 'want.csv';
 set have;
 if _n_=1 then put 'No particular amount';
 put no particular amount;
 format _character_ $quote.;
run;
No particular amount
1 "I BOOKS" 5000
2 " A PLANE" 3000
3 " B DOUBLE ROOL" 2000
4 "II STATENAORY" 1000
5 " A PEN" 500
6 " B PENCIL" 500

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 503 views
  • 2 likes
  • 4 in conversation