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

Hello everybody,

 

I make some data with QUOTE function. In my output, the csv file, I have the correct values like "City".

When I convert the csv to txt file, I have the values like ""City"".

I would like to have the same values ("City") in csv and txt files. Does it possible?

 

Thank you for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

@SASdevAnneMarie wrote:
Thank you for your answer.
When I don't include the additional quotes to csv, don't have the quotes in txt. As you wrote, I must have one.

It includes quotes on fields that require them, not all fields. So if a field has a comma or quote, then it will get masked, otherwise it won't. You don't necessarily need all fields quotes in your export. If you want that, use a data step to export your data.

 

Run both examples below, changing the paths to what's appropriate for you use case of course.

 

data demo;
length name $20. sex $5.;
set sashelp.class;
if name ='Alfred' then name = 'Al,Fred';
if name='Jane' then name = "Ja'ne";
*name = quote(trim(name));
*sex=quote(trim(sex));
run;

proc export data=demo outfile="/home/fkhurshed/Demo1/demo1.csv" dbms=csv replace;run;


data demo;
length name $20. sex $5.;
set sashelp.class;
if name ='Alfred' then name = 'Al,Fred';
if name='Jane' then name = "Ja'ne";
name = quote(trim(name));
sex=quote(trim(sex));
run;

proc transpose data=demo(obs=0) out=names;
  var _all_;
run;

data _null_ ;
  file "/home/fkhurshed/Demo1/demo2.csv" dsd ;
  set names end=eof;
  put _name_ @ ;
  if eof then put;
run;


data _null_;
set demo;
  file "/home/fkhurshed/Demo1/demo2.csv" dlm=',' MOD;
 put (_all_) (+0);
run;

 

 

 

View solution in original post

11 REPLIES 11
Jagadishkatam
Amethyst | Level 16

Could you please share the code you used to convert the csv to txt file

Thanks,
Jag
SASdevAnneMarie
Barite | Level 11
Thank you for your message. I do the conversation manually.
Reeza
Super User

If you use PROC EXPORT or any of the automated exports to a CSV it will automatically encapsulate any field that requires it with quotes. If you've already included quotes, you need to use a data step to export your data instead or rely on SAS automated exports. 

 


@SASdevAnneMarie wrote:

Hello everybody,

 

I make some data with QUOTE function. In my output, the csv file, I have the correct values like "City".

When I convert the csv to txt file, I have the values like ""City"".

I would like to have the same values ("City") in csv and txt files. Does it possible?

 

Thank you for your help!


 

SASdevAnneMarie
Barite | Level 11
Thank you for your answer.
When I don't include the additional quotes to csv, don't have the quotes in txt. As you wrote, I must have one.
Tom
Super User Tom
Super User

@SASdevAnneMarie wrote:
Thank you for your answer.
When I don't include the additional quotes to csv, don't have the quotes in txt. As you wrote, I must have one.

Please explain in detail what you are trying to achieve. You can ask the SAS data step to add quotes by using the ~ modifier in the PUT statement.

Example:

2271  data _null_;
2272   set sashelp.class (obs=3);
2273   file log dsd ;
2274   put name age sex ~ height weight ;
2275  run;

Alfred,14,"M",69,112.5
Alice,13,"F",56.5,84
Barbara,13,"F",65.3,98
NOTE: There were 3 observations read from the data set SASHELP.CLASS.

 

Reeza
Super User

@SASdevAnneMarie wrote:
Thank you for your answer.
When I don't include the additional quotes to csv, don't have the quotes in txt. As you wrote, I must have one.

It includes quotes on fields that require them, not all fields. So if a field has a comma or quote, then it will get masked, otherwise it won't. You don't necessarily need all fields quotes in your export. If you want that, use a data step to export your data.

 

Run both examples below, changing the paths to what's appropriate for you use case of course.

 

data demo;
length name $20. sex $5.;
set sashelp.class;
if name ='Alfred' then name = 'Al,Fred';
if name='Jane' then name = "Ja'ne";
*name = quote(trim(name));
*sex=quote(trim(sex));
run;

proc export data=demo outfile="/home/fkhurshed/Demo1/demo1.csv" dbms=csv replace;run;


data demo;
length name $20. sex $5.;
set sashelp.class;
if name ='Alfred' then name = 'Al,Fred';
if name='Jane' then name = "Ja'ne";
name = quote(trim(name));
sex=quote(trim(sex));
run;

proc transpose data=demo(obs=0) out=names;
  var _all_;
run;

data _null_ ;
  file "/home/fkhurshed/Demo1/demo2.csv" dsd ;
  set names end=eof;
  put _name_ @ ;
  if eof then put;
run;


data _null_;
set demo;
  file "/home/fkhurshed/Demo1/demo2.csv" dlm=',' MOD;
 put (_all_) (+0);
run;

 

 

 

SASdevAnneMarie
Barite | Level 11
Thank you very much,Reeza, it works.
I understand the difference beetwing using the proc export and file export.

Best regards,
Marie
Reeza
Super User

@SASdevAnneMarie wrote:
Thank you for your answer.
When I don't include the additional quotes to csv, don't have the quotes in txt. As you wrote, I must have one.

No, you do not require quotes around all character variables in a well formed CSV. You only require quotes around fields that have an embedded character that could cause issues such as a comma or quote. 

You do not require them around all fields. That's the approach many users take rather than bother to figure out which observations need to be masked. It's easier to include quotes around everything, but that isn't necessarily 'better'. It makes the file larger by default. 

Tom
Super User Tom
Super User

@SASdevAnneMarie wrote:

Hello everybody,

 

I make some data with QUOTE function. In my output, the csv file, I have the correct values like "City".

When I convert the csv to txt file, I have the values like ""City"".

I would like to have the same values ("City") in csv and txt files. Does it possible?

 

Thank you for your help!


It is not at all apparent what you are talking about. Can you provide example data and programs?  Please post the information using the Insert Code or Insert SAS code buttons to preserve the formatting of the text.  Copy in a program to create the sample SAS dataset. Program to create the CSV file. And examples of what you want and how what you are getting is different than what you want.  You probably need only create two or three lines of data for two or three variables to represent your issue.

 

Note that a CSV files IS a text file.  It is just one that has commas between the values, Comma Separated Values.  If the values contain the comma then they value needs to be in quotes. So if the value for a field is:

1,234

then it will be represented in the file as

"1,234"

so that it is not considered two values instead of one.

 

That means that if you want to have actual quotes in the data then they also need to be in quotes to prevent them from being interpreted as quotes meant to protect the embedded commas.  Once they are in quotes then the actual quotes need to be doubled so that your can tell they are part of the data and not just the quotes added to protect the commas.  So if you data is 

"city"

then in the CSV file this needs to be represented by 

"""city"""

The outer quotes will be removed and the inner ones duplicates will be replaced.

ChrisNZ
Tourmaline | Level 20

> When I convert the csv to txt file

This phrase makes no sense: CSV files are text files.

 

You really must explain and show us what exactly you are doing.

SASdevAnneMarie
Barite | Level 11
Hello,

Thank you for your message. Sorry, It wasn't clear. I meant when I change .csv in .txt format manually.

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