BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ramkal21
Obsidian | Level 7

Hello all,

I was wondering if SAS has a capability to automatically add Quotes or Double quotes to output fields values based on say if the field is of Character or Numeric or Packed decimal value

 

Say if the field is CHAR

Name

Ivy

 

I am expecting the output in the file as below

"Ivy"

 

If the field values are not CHAR then it should not add the double quotes.

 

Thanks,

Ramanujam

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Let the QUOTE function do all the work.

https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/p059exu866hqw2n0zw9aoxf3p6oj.htm

 

Which brings up the question ... why would this be helpful? Why do you need text inside double or single quotes?

--
Paige Miller

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

Let the QUOTE function do all the work.

https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/p059exu866hqw2n0zw9aoxf3p6oj.htm

 

Which brings up the question ... why would this be helpful? Why do you need text inside double or single quotes?

--
Paige Miller
ramkal21
Obsidian | Level 7

Thanks @PaigeMiller  for your quick response. I am trying to mimic output from a vendor product utility (which is being replaced) and that utility automatically identifies fields datatypes based on it's values.

 

Other than QUOTE, could you let me know if there is any other option?

PaigeMiller
Diamond | Level 26

What is wrong with QUOTE? Please provide details.

--
Paige Miller
ramkal21
Obsidian | Level 7

Thanks @PaigeMiller and @Reeza.  I was able to get my need satisfied by using $QUOTE format (please see below link)., basically was trying to reduce lines of code. Using $QUOTE format eliminated the need to have a separate line for QUOTE function.   https://documentation.sas.com/doc/en/vdmmlcdc/8.1/ds2ref/p180g91lfrpgfnn0zw28u8hwu3mt.htm#:~:text=Wr....

 

var1 = QUOTE(var2);

 

replaced it as

 

put var1 $quote8.;

 

My output is a comma delimited file and using 'Quotes' for characters eliminates the confusion when the file is imported at the receiving end.

Hope this clarifies.

 

Thank you both again for leading me to the right solution.

 

Regards,

Ramanujam

Reeza
Super User
You can easily customize the output format of a variable, even automate it dynamically. However, you'll need to show an example of what you're trying to achieve if you want any examples otherwise, a vague question (is something possible) gets a vague answer(yes, it's possible).
Tom
Super User Tom
Super User

@ramkal21 wrote:

Thanks @PaigeMiller  for your quick response. I am trying to mimic output from a vendor product utility (which is being replaced) and that utility automatically identifies fields datatypes based on it's values.

 

Other than QUOTE, could you let me know if there is any other option?


I assume you mean that what ever process is READING the files produced by the thing being replaced is currently GUESSING that quoted values should be character variables?  Having a system that depends on guessing is not really a good idea.  Can you fix the receiver side also to accept a sperate file with the variable definitions?  Then you could just write a normal CSV file were quotes are only added when the are NEEDED.

 

If you are writing a CSV file then you probably want to use the ~ format modifier in the PUT statement and not the QUOTE() function of $QUOTE format.

 

data _null_;
  file OUT dsd ;
  set sashelp.class ;
  put name ~ sex ~ age height weight;
run;
"Alfred","M",14,69,112.5
"Alice","F",13,56.5,84
"Barbara","F",13,65.3,98
"Carol","F",14,62.8,102.5
"Henry","M",14,63.5,102.5
"James","M",12,57.3,83
"Jane","F",12,59.8,84.5
"Janet","F",15,62.5,112.5
"Jeffrey","M",13,62.5,84
"John","M",12,59,99.5
"Joyce","F",11,51.3,50.5
"Judy","F",14,64.3,90
"Louise","F",12,56.3,77
"Mary","F",15,66.5,112
"Philip","M",16,72,150
"Robert","M",12,64.8,128
"Ronald","M",15,67,133
"Thomas","M",11,57.5,85
"William","M",15,66.5,112

You could do a little work to help generate that automatically if you want.

proc sql noprint;
select catx(' ',nliteral(name),case when type='char' then '~' else ' ' end)
  into :varlist separated by ' '
  from dictionary.columns
  where libname='SASHELP' and memname='CLASS'
  order by varnum
;
quit;

data _null_;
  file log dsd ;
  set sashelp.class;
  put &varlist;
run;

You could also try using ODS CSV as it has the nasty habit of adding extra quotes like that.

filename out temp;
ods csv file=out;
proc print data=sashelp.class noobs; run;
ods csv close;

Result

"Name","Sex","Age","Height","Weight"
"Alfred","M",14,69.0,112.5
"Alice","F",13,56.5,84.0
"Barbara","F",13,65.3,98.0
"Carol","F",14,62.8,102.5
"Henry","M",14,63.5,102.5
"James","M",12,57.3,83.0
"Jane","F",12,59.8,84.5
"Janet","F",15,62.5,112.5
"Jeffrey","M",13,62.5,84.0
"John","M",12,59.0,99.5
"Joyce","F",11,51.3,50.5
"Judy","F",14,64.3,90.0
"Louise","F",12,56.3,77.0
"Mary","F",15,66.5,112.0
"Philip","M",16,72.0,150.0
"Robert","M",12,64.8,128.0
"Ronald","M",15,67.0,133.0
"Thomas","M",11,57.5,85.0
"William","M",15,66.5,112.0

 

 

Reeza
Super User
Define automatically? What do you mean by output? SAS really only has numeric/character.
Patrick
Opal | Level 21

Should this quoting be based on variable data type of which SAS 9.n only got CHAR and NUM or based on actual values meaning for the same variable there could be cases with and without quotes based on the actual string?

 

You need to share more detail as well as some representative sample data (please provided via a tested SAS datastep with datalines) and desired result based on the sample data. 

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
  • 9 replies
  • 1611 views
  • 7 likes
  • 5 in conversation