HI,all
the question is “Why I use the catx function couldn not get the results as use the symbol "||" which is without blank between two variables"
THANKS.
/*the variable "aestart" is uesd code as follows:*/
if ^missing(aestdat) then do
aestart=put(aestdat,yymmdd10.)||"T"||aesttim;
/*the variable "aa" by using the catx function:*/
aa=catx("",aestart1,"T",aesttim);
the results as follow
Convert your separate date and time into a datetime column, then use the appropriate format, instead of trying to roll your own:
data have;
date=date();
time=time();
format date date. time time.;
run;
data want;
set have;
datetime=dhms(date,0,0,time);
datetime2=datetime;
format datetime datetime.;
format datetime2 e8601dt.;
run;
If you REALLY want to do what you're trying: 1) read the doc on the CATX function, and 2) use the CATS function instead, or 3) use CATX("T",date,time).
But IMO you should store dates as dates, times as times, and datetimes as datetimes, instead of character string representations of your data.
The CATX function first copies item-1 to the result, omitting leading and trailing blanks. Next, for each subsequent argument item-i, i=2, …, n, if item-i contains at least one non-blank character, CATX appends delimiter and item-i to the result, omitting leading and trailing blanks from item-i. CATX does not insert the delimiter at the beginning or end of the result. Blank items do not produce delimiters at the beginning or end of the result, nor do blank items produce multiple consecutive delimiters.
Does that clarify it for you?
I think you've used CATX incorrectly though.
The delimiter is supposed to be the FIRST parameter, not the third. I think you want:
aa = catx("T", aestart1, aesttim);
or alternatively use the CATT function:
aa = catt(aestart1, "T", aesttim);
@HannaZhang wrote:
HI,all
the question is “Why I use the catx function couldn not get the results as use the symbol "||" which is without blank between two variables"
THANKS.
/*the variable "aestart" is uesd code as follows:*/
if ^missing(aestdat) then do
aestart=put(aestdat,yymmdd10.)||"T"||aesttim;
/*the variable "aa" by using the catx function:*/
aa=catx("",aestart1,"T",aesttim);
the results as follow
And... @ScottBass is correct, you should be storing your date time as a SAS date time variable not as a character variable.
I would recommend the following code instead:
aa =dhms(input(
substr(aestart1, 1, 10), yymmdd10.)
),
0,
0,
input(
coalesce(aesttim, 0), time.)
)
);
format aa datetime20.;
thanks
Convert your separate date and time into a datetime column, then use the appropriate format, instead of trying to roll your own:
data have;
date=date();
time=time();
format date date. time time.;
run;
data want;
set have;
datetime=dhms(date,0,0,time);
datetime2=datetime;
format datetime datetime.;
format datetime2 e8601dt.;
run;
If you REALLY want to do what you're trying: 1) read the doc on the CATX function, and 2) use the CATS function instead, or 3) use CATX("T",date,time).
But IMO you should store dates as dates, times as times, and datetimes as datetimes, instead of character string representations of your data.
You've stumbled across a peculiarity of SAS when it comes to "empty" strings. Missing character variables are padded with strings, and SAS inserts single blanks as delimiters (eg when you put variables with the put statement) if no other delimiter is explicitly specified.
In your case, by using an empty string as the first parameter in catx, you have made SAS insert single blanks.
You should have used either
aa = catx("T",aestart1,aesttim);
or
aa = cats(aestart1,"T",aesttim);
to avoid these blanks.
The really correct method is, of course, to use SAS date and time values and the formats that SAS provides for such values, as is done in the accepted solution.
Thanks a lot,I think I know what my Mental flaw is.
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.