DATA Step, Macro, Functions and more

Create quoted flat file headers

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

Create quoted flat file headers

Dear all,

I finally discovered that using a tilde put quotes around the values.

Perfect!!

Now I still what quotes around the headers.

I can use the quote function in the query  quote(trim(name))  . . and yes quotes are added but  the PUT statement still
fails in the data _null_ step.

    

Suggestions???

proc sql noprint;
select trim(name) into :vars separated by ';'
from dictionary.columns
where libname="SASHELP"    & memname="CLASS"
order by varnum ;
quit;

filename temp 'c:\temp\test.txt' ;

data _null_;
  file temp dlm=";" dsd lrecl=3000;
  put "&vars."  ;
  do until (eof);
    set SASHELP.CLASS end=eof;
    put (_all_)(~);
  end;
  stop;
run;

GreetZ,

Herman


Accepted Solutions
Solution
‎12-16-2014 07:47 AM
Respected Advisor
Posts: 3,777

Re: Create quoted flat file headers

You need to make the syntax correct by doubling the double quotes inside the quoted string in the PUT statement.  Use the QUOTE function again on VARS.

proc sql noprint;
  
select quote(trim(name)) into :vars separated by ';'
  
from dictionary.columns where libname="SASHELP"    & memname="CLASS" order by varnum ;
  
quit;
%put NOTE: %superq(vars);

filename temp temp;*'c:\temp\test.txt' ;
data _null_;
  
file temp dlm=";" dsd lrecl=3000;
  
put %sysfunc(quote(%superq(vars)));
   do until (eof);
      set SASHELP.CLASS end=eof;
      put (_all_)(~);
      end;
  
stop;
  
run;
data _null_;
  
infile temp;
  
input;
  
put _infile_;
  
run;


12-16-2014 6-45-37 AM.png

View solution in original post


All Replies
Super User
Super User
Posts: 7,430

Re: Create quoted flat file headers

Well, I am sure there is a better way, however you can generate the necessary code rather than using a macro variable, something like:

filename temp 's:\temp\rob\test.txt' ;

data temp;
  set sashelp.vcolumn (where=(libname="SASHELP" and memname="CLASS")) end=last;
  length lst_list $2000.;
  retain lst_list;
  if _n_=1 then lst_list='"'||strip(name)||'";';
  else lst_list=strip(lst_list)||'"'||strip(name)||'";';
  if last then do;
    call execute('data _null_; file temp dlm=";" dsd lrecl=3000; set sashelp.class;');
    call execute(" put '"||strip(lst_list)||"';");
    call execute('; put (_all_)(~); run;');
  end;
run;

Solution
‎12-16-2014 07:47 AM
Respected Advisor
Posts: 3,777

Re: Create quoted flat file headers

You need to make the syntax correct by doubling the double quotes inside the quoted string in the PUT statement.  Use the QUOTE function again on VARS.

proc sql noprint;
  
select quote(trim(name)) into :vars separated by ';'
  
from dictionary.columns where libname="SASHELP"    & memname="CLASS" order by varnum ;
  
quit;
%put NOTE: %superq(vars);

filename temp temp;*'c:\temp\test.txt' ;
data _null_;
  
file temp dlm=";" dsd lrecl=3000;
  
put %sysfunc(quote(%superq(vars)));
   do until (eof);
      set SASHELP.CLASS end=eof;
      put (_all_)(~);
      end;
  
stop;
  
run;
data _null_;
  
infile temp;
  
input;
  
put _infile_;
  
run;


12-16-2014 6-45-37 AM.png
Super User
Posts: 9,691

Re: Create quoted flat file headers

Why not use ods csv ?

ods csvall file='c:\temp\x.csv' options(doc="help" Delimiter=';' Quoted_columns="123456789");
 title ' ';
 proc print data=sashelp.class noobs;run;
ods csvall close;

Xia Keshan

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 337 views
  • 1 like
  • 4 in conversation