SAS (Base SAS)+
data alldata;
infile cards dsd dlm='|' truncover;
input dtc : $20.;
cards;
2025
2025-01
2025-01-10
2025-02-28
2025-03
2025-12-31
2025-12-31T23
2025-12-31T23:59
2025-12-31T23:59:59
.
;
run;
data dates01;
set alldata;
length=length(dtc);
countw=countw(dtc,'T');
scan_fword=scan(dtc,1,'T');
countc=countc(dtc,'-');
findt=find(dtc,'T','i');
substr_date=substr(dtc,1,10);
concat=catx(" ", "Event start:",dtc);
translate=translate(dtc,'-',':');
tranwrd=tranwrd(dtc,'2025','20xx');
missing=missing(dtc);
run;- This lesson demonstrates the most common SAS character functions by applying them to a column of ISO-8601 partial dates.
LENGTH(dtc)returns the number of characters in the value — useful for spotting truncated or oversized strings.COUNTW(dtc, 'T')counts words whenTis treated as a delimiter — so "2025-12-31T23:59" has two words.SCAN(dtc, 1, 'T')grabs the first word with the same delimiter — the date portion before theT.COUNTC(dtc, '-')counts occurrences of the dash character — handy for distinguishing year-only, year-month and full date formats.FIND(dtc, 'T', 'i')returns the character position of theTseparator, or 0 if absent.SUBSTR(dtc, 1, 10)extracts the first 10 characters — the full date portion when present.CATX(" ", "Event start:", dtc)joins values with a space separator, quietly skipping empty pieces.TRANSLATE(dtc, '-', ':')swaps every colon with a dash, character-by-character.TRANWRD(dtc, '2025', '20xx')replaces a whole word or substring with another — the go-to for string redaction.MISSING(dtc)returns 1 if the value is missing and 0 otherwise — safer than testingdtc = "".- After running, we inspect
dates01and confirm each new column reflects the transformation its name suggests.