BookmarkSubscribeRSS Feed
danfavero
Calcite | Level 5

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"

11 REPLIES 11
Haikuo
Onyx | Level 15

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

RamKumar
Fluorite | Level 6

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?

data_null__
Jade | Level 19

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
    Ksharp
    Super User
    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

    LineMoon
    Lapis Lazuli | Level 10

    @Ksharp: It seems to be a nice solution.

    It will work with sas 9.2 ?

     

    LineMoon
    Lapis Lazuli | Level 10
    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



    Ksharp
    Super User

    Then try HaiKuo's code.

    LineMoon
    Lapis Lazuli | Level 10
    t hank you
    but i have a data set...i do ont have a cards
    Ksharp
    Super User
    
    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;
    

    sas-innovate-2024.png

    Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

    Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

     

    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
    • 11 replies
    • 17393 views
    • 3 likes
    • 7 in conversation