Using regular datetime variables, I can can't get SQL to produce this
02FEB20:15:20:00,12JUN20:15:26:50,24JUL20:16:20:12
from this code:
proc sql noprint;
select distinct sample_dtm into :vardtm separated by ","
from sampleID_database;
quit;
%put &vardtm;
so I assume that sample_dtm is a character variable. In that case, you can use the CATS function as below:
proc sql noprint;
select distinct cats("'",sample_dtm,"'dt") into :vardtm separated by ","
from sampleID_database;
quit;
%let ndates=&sqlobs;
%put &=vardtm;
%put &=ndates;
which, with my sample data produces
359 %put &=vardtm;
VARDTM='01JAN2018:13:00:00'dt,'02JAN2018:14:00:00'dt,'03JAN2018:15:00:00'dt,'04JAN2018:16:00:00'dt
,'05JAN2018:17:00:00'dt
360 %put &=ndates;
NDATES=5
in the log.
Now you have a comma-separated list of date time literals in your macrovar VARDTM that you can loop through, which should not require any further transformation to be useful in subsequent code.
And you also know the number of literals from the NDATES macrovar.
And the other advantage of this is that the macro values are human readable.