BookmarkSubscribeRSS Feed
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 15 replies
  • 1122 views
  • 10 likes
  • 5 in conversation