BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
HannaZhang
Obsidian | Level 7

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

 

1.png

1 ACCEPTED SOLUTION

Accepted Solutions
ScottBass
Rhodochrosite | Level 12

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.

 


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

View solution in original post

7 REPLIES 7
Reeza
Super User

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

 

1.png


 

Reeza
Super User

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.;
HannaZhang
Obsidian | Level 7

thanks

ScottBass
Rhodochrosite | Level 12

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.

 


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
HannaZhang
Obsidian | Level 7
Hi,ScottBass,
Thanks for your solution  to  my question ,It really helped me solve the problem.By the way, thanks for your suggestion to my post.
Kurt_Bremser
Super User

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.

HannaZhang
Obsidian | Level 7

Thanks a lot,I think I know what my Mental flaw is.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 3099 views
  • 4 likes
  • 4 in conversation