I am trying to write a SAS script that will simply read in a SAS .sas7bdat data file and output it in text format. I want dates to be output in YYYYMMDD format. I don't know what the names of the date columns will be. My script is currently:
libname tmplib '~/testdatadir/';
OPTIONS MISSING='00'x;
data tmpdata;
set tmplib.testdatafile;
array flds{*} _NUMERIC_;
do i=1 to dim(flds);
if missing(flds(i)) then flds(i)=.;
end;
array charflds{*} _CHARACTER_;
do i=1 to dim(charflds);
if missing(charflds(i)) then charflds(i)=' ';
end;
drop i;
RUN;
PROC EXPORT
DATA = tmpdata
OUTFILE = 'testdataoutfile.txt'
DBMS = TAB REPLACE;
PUTNAME = YES;
RUN;
I would either like to iterate through all date fields (as I do with NUMERIC fields and CHARACTER fields), or add a check for each NUMERIC field testing whether it is a date (then I could change the format), or add an option to PROC EXPORT to indicate the output date format. Any other approach to get the output file to have dates formatted as YYYYMMDD would be acceptable as well.
You can use the varfmt function to find the format of a numeric variable, which can help you determine whether it’s a date, i.e. whether its format is a date format. Technically you could have a date that isn’t in a correct date format, so it’s just displayed as 16239 or something, but those are hard to detect because it could also just be the number 16,239. This method will find anything that’s displayed as a date in the data browser.
Then use some combination of put and input to get it into YYYYMMDD format.
One issue with just using put is that I think by default it will return a character variable, so you’ll either need to create a new character variable to hold the date, or convert the YYYYMMDD back into an 8-digit number, so the number 20120501 instead of the character string 20120501.
For example 2, you should note that varfmt returns the format of a variable given the dataset name and a variable number. In the example they set up a separate vars table whose purpose is to loop through all the variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With