youji.xii
youji.xii

Reputation: 96

Writing from proc sql to file

I am new to SAS and I need to write a single value into a file from proc sql but I am having so much trouble. Please see my code below.

data _null_;
FILE 'C:\MyFile.txt';

proc sql;
  select datepart(max(DateCreated))  into :dt from epx.temp;
  %put &dt date9.;
quit;

run;

I am running this code inside a MACRO in Precode and Postcode property of a job.

Upvotes: 2

Views: 1826

Answers (1)

DWal
DWal

Reputation: 2762

The proc sql statement is not valid inside the data step. Actually, SAS recognizes a proc as the end boundary of the data step so your data step contains only the file statement and does nothing.

The file statement affects the put statement, not the macro %put statement.

Without changing your general approach, you can do the following:

Put the formatted value into a macro variable then use the data step to write the value to your file.

proc sql;
select put(datepart(max(DateCreated)),date9.) into :dt from ext.temp;
quit;

data _null_;
  FILE 'C:\MyFile.txt';
  put "&dt";
run;

Another approach would be to do everything in the data step and avoid using macro variables altogether:

data _null_;
  file 'C:\MyFile.txt';
  set ext.temp end=eof;
  retain maxdate;
  if datepart(DateCreated) gt maxdate then maxdate=datepart(DateCreated);
  if eof then put maxdate date9.;
run;

Upvotes: 2

Related Questions