BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
buddha_d
Pyrite | Level 9

Community, 

           I am just trying to figure out how to write a string to .csv file. Here is my attempt.

data one;
input (acct_id gender)($) charge $20.;
datalines;
101 M 20000,10000,20000
102 F 20000,21000,11000
103 F 50000,18000,49000
;
run;

data _null;
	set one;
	file 'C:\statements\expenses.csv';
	if _n_=1 then do;
	put "Acct_id,Gender,Expenses";
	end;
	put Acct_id ',' gender ',' charge ;
run;

The output in csv file I want it to look like exactly how the input data looked like. The commas are breaking in each cell (for example the first row with charge should look like 20000,10000,20000 in a single excel cell instead of showing up as 20000 10000 20000 in 3 different cells). I can't think of a way to do it. Any suggestions please 

Thanks,

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You don't have to add the commas, just tell the data step you want to write a delimited file by adding the DSD option to the FILE statement.  Then it will automatically add the quotes for any values that have commas in them.

58    data _null;
59      set one;
60     * file 'C:\statements\expenses.csv' dsd ;
61      file log dsd;
62      if _n_=1 then put "Acct_id,Gender,Expenses";
63      put Acct_id  gender charge ;
64    run;

Acct_id,Gender,Expenses
101,M,"20000,10000,20000"
102,F,"20000,21000,11000"
103,F,"50000,18000,49000"

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

You don't have to add the commas, just tell the data step you want to write a delimited file by adding the DSD option to the FILE statement.  Then it will automatically add the quotes for any values that have commas in them.

58    data _null;
59      set one;
60     * file 'C:\statements\expenses.csv' dsd ;
61      file log dsd;
62      if _n_=1 then put "Acct_id,Gender,Expenses";
63      put Acct_id  gender charge ;
64    run;

Acct_id,Gender,Expenses
101,M,"20000,10000,20000"
102,F,"20000,21000,11000"
103,F,"50000,18000,49000"
buddha_d
Pyrite | Level 9

Thanks Tom for quick reply.

It works perfectly fine for Log window. But, with excel it shows differently. Any other way to work around with excel output? 

Thanks again

Tom
Super User Tom
Super User

@buddha_d wrote:

Thanks Tom for quick reply.

It works perfectly fine for Log window. But, with excel it shows differently. Any other way to work around with excel output? 

Thanks again


 

If you remove the FILE LOG statement and uncomment the other FILE statement it will write a CSV file.

Note that a CSV is just a text file.  How does EXCEL come into the question?  Are you opening the CSV file with Excel? HOW are you opening the CSV file with Excel. Also make sure you don't still have the file open in Excel when you run the data step or you will probably get an error that SAS could not open the file for writing.

 

Personally I never let Excel open a CSV file by itself because it will in general mangle the data.  Instead open Excel and then ask it to import the CSV file and it will give you options to control how it converts the text into cell values.

buddha_d
Pyrite | Level 9

Thank Tom for the clarification. My computer defaults .csv files to excel, but .txt file seems alright. 

Thanks for your help..

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1763 views
  • 0 likes
  • 2 in conversation