Desktop productivity for business analysts and programmers

How to export a txt with double quotes

Reply
Occasional Contributor
Posts: 7

How to export a txt with double quotes

Does anyone know by any chance how to export a txt file with double quotes and when the text field is blank it be

exported as ""?

For example:

field1field2field3
abc123XXX
def456
ghi654YYY

I would like to export a txt file like this:

field1;field2;field3

"abc";123;"XXX"

"def";456;""

"ghi";654;"YYY"

Trusted Advisor
Posts: 3,215

Re: How to export a txt with double quotes

Posted in reply to danfavero
Respected Advisor
Posts: 3,157

Re: How to export a txt with double quotes

Posted in reply to danfavero

Jaap's suggestion certainly works. Here is an alternative, You will need 'FILENAME' statement to address your destination instead of 'LOG':

data have;

     infile cards truncover;

     input (field1   field2     field3) (:$8.);

     file log dsd dlm=';';

     put field1 ~ field2 field3 ~;

     cards;

abc 123 XXX

def 456 .

ghi 654 YYY

;

Regards,

Haikuo

Regular Contributor
Posts: 168

Re: How to export a txt with double quotes

In your code, whether ~ is used to create file with double quotes? If so, what symbol to use for producing a file with single quotes?

Respected Advisor
Posts: 3,808

Re: How to export a txt with double quotes

I don't know of a symbol for single quotes.  You could do the single quoting with the quote function but you much also make sure the character variables have adequate to hold the two or more characters that will be added to the value.  This would be easier if there were an $SQUOTE function similar to $QUOTE then you would not have the length problem.  I suppose $SQUOTE could be written using PROC FMCP, we can leave that for another time.

filename FT66F001 '~/sashelp.csv' lrecl=256;
data _null_;
  
length Name $10 Sex $3;
  
set sashelp.class;
   if _n_ in(2 4 8) then call missing(of _char_);
   array c
  • _character_;
      
    file FT66F001 dlm=';' termstr=crlf;
       if _n_ eq 1 then link names;
       do _n_ = 1 to dim(c);
          c[_n_] = quote(strip(c[_n_]),"'");
          end;
      
    put (_all_)(+0);
       return;
    names:
      
    length _name_ $32;
      
    do while(1);
          call vnext(_name_);
          if upcase(_name_) eq '_NAME_' then leave;
          put _name_ @;
          end;
      
    put;
      
    return;
      
    run;

    Name;Sex;Age;Height;Weight
    'Alfred';'M';14;69;112.5
    '';'';13;56.5;84
    'Barbara';'F';13;65.3;98
    '';'';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
    '';'';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
    Super User
    Posts: 10,313

    Re: How to export a txt with double quotes

    Posted in reply to danfavero
    data have;
         input field1 $  field2     field3 $;
         cards;
    abc 123 XXX
    def 456 .
    ghi 654 YYY
    ;
    run;
    ods listing close;
    ods csv file='c:\temp\y.txt' options(doc='help' Delimiter=';');
    proc print data=have label noobs;run;
    ods csv close;
    ods listing;
    
    

    Another way :

    %ds2csv (data=have, runmode=b, csvfile=c:\temp\retail.csv);

    Xia Keshan

    Super Contributor
    Posts: 371

    Re: How to export a txt with double quotes

    @Ksharp: It seems to be a nice solution.

    It will work with sas 9.2 ?

     

    Super User
    Posts: 10,313

    Re: How to export a txt with double quotes

    Sure.
    
    
    Super Contributor
    Posts: 371

    Re: How to export a txt with double quotes

    Posted in reply to danfavero
    thank you
    but i need To have this

    id;name;
    "12";"bob";

    for thé moment i cet this
    "id";"name";
    "12";"bob";
    si I do ont lIke that



    Super User
    Posts: 10,313

    Re: How to export a txt with double quotes

    Then try HaiKuo's code.

    Super Contributor
    Posts: 371

    Re: How to export a txt with double quotes

    Posted in reply to danfavero
    t hank you
    but i have a data set...i do ont have a cards
    Super User
    Posts: 10,313

    Re: How to export a txt with double quotes

    
    filename x 'c:\temp\xx.csv';
    data _null_;
         set have;
         file x dsd dlm=';';
    if _n_=1 then put 'field1' 'field2' 'field3';
    put field1 ~ field2 field3 ~;
    run;
    
    Ask a Question
    Discussion stats
    • 11 replies
    • 6386 views
    • 2 likes
    • 7 in conversation