I am new to SAS. I am trying to write to a txt file the following date format. 2015-47-31.06.47.31.235647. I am reading the date from a DB2 table and trying to write out the records to a txt file. I have tried different methods and read different threads. But no thread gave me the solution to the format that I want. the closest I came to what I need is with the following format 2015-47-31.06.47.31 using the below code.
PROC FORMAT;
PICTURE DATETS OTHER = '%0Y-%0M-%0D.%0H.%0M.%0S' (datatype=datetime);
RUN;
PROC PRINT DATA= EXAMPLE01;
FORMAT ACC_ENY_ACT_TS DATETS.;
RUN;
Any help is greatly appreciated
Hello @GTS and welcome to the SAS Support Communities!
I think you're almost there. Note that the directives for date and time values are case sensitive, which is particularly important because "M" stands for minute and "m" for month.
/* Create the picture format */
proc format;
picture datets (default=26)
other = '%Y-%0m-%0d.%0H.%0M.%0s' (datatype=datetime);
run;
/* Test the picture format */
data _null_;
dt1='31JUL2015:06:47:31.235647'dt;
put dt1 datets26.6;
dt2='01AUG2015:23:04:05.067890'dt;
put dt2 datets.6;
run;
Defining the default length (26) is optional, but it allows for an abbreviated format specification (omitting the length) as shown in the second example. The number of decimals (6) must be specified, though, to display the fractional seconds.
Did the Proc Print output look like you needed?
If the destination is truly a TXT file then wrap the code in an ODS LISTING to write to the desired file.
ods listing file="<path>\file.txt"; PROC PRINT DATA= EXAMPLE01; FORMAT ACC_ENY_ACT_TS DATETS.; RUN; ods listing close;
of course you would use a path and file name of your choice.
If the output wasn't as you desired you'll have to provide more details of what is wrong.
@GTS wrote:
Hello. I tried what you sent and it gave me the date format I needed. Thank you. I need to work some more on learning the ODS listing functions because the next step is to created a fixed length txt file.
Fixed length files will usually involve the PUT statement with column pointers and a FILE statement in a data step.
There are lots of examples around. Basically:
data _null_; set sashelp.class; file "path\filename.txt" lrecl=100; put @1 name @10 sex @15 age @20 weight f5.2 @30 height f4.1 ; run;
Of course set your datasource path, filename. LRECL should be as long as the longest expected line.
The @ is which column to start writing a value then the name of a variable and optionally the format associated.
If you need a header row that would look like this before the above PUT statement:
If _n_=1 then Put "This is the text of a single header row, whatever it may be";
The _n_ tests for which line of data from the data set you are on and only executes the PUT statement for the first record.
If you need more rows the code would be a then do; <multiple PUT statements.> end;
Hello @GTS and welcome to the SAS Support Communities!
I think you're almost there. Note that the directives for date and time values are case sensitive, which is particularly important because "M" stands for minute and "m" for month.
/* Create the picture format */
proc format;
picture datets (default=26)
other = '%Y-%0m-%0d.%0H.%0M.%0s' (datatype=datetime);
run;
/* Test the picture format */
data _null_;
dt1='31JUL2015:06:47:31.235647'dt;
put dt1 datets26.6;
dt2='01AUG2015:23:04:05.067890'dt;
put dt2 datets.6;
run;
Defining the default length (26) is optional, but it allows for an abbreviated format specification (omitting the length) as shown in the second example. The number of decimals (6) must be specified, though, to display the fractional seconds.
Thank you so much. What you sent worked. I guess because I am new to SAS I am having difficulties trying to find the appropriate help topics to solve my issues and answer my questions. Thanks again
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.