BookmarkSubscribeRSS Feed
shubham1
Calcite | Level 5

data new;

x=' ';

z='abc';

run;

 

data new1;

set new;

zz=catx(',',x,z);

run;

 

I am trying to run this code but when I am opening the dataset new 1 

Variable ZZ has value abc .It is not having ,

 

why ZZ is not having a value ,abc?

 

 

 

 

9 REPLIES 9
Shmuel
Garnet | Level 18

Catx function removes leading and trailing spaces from concatenated strings.

shubham1
Calcite | Level 5
hello

I agree to you point that It removes leading and trailing spaces.But i have used the delimiter , in below statement
zz=catx(',',x,z);

Why comma(,) is not coming in the output ?
gamotte
Rhodochrosite | Level 12

Hello,

The catx function has been designed that way.

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarg...

 

The Basics

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.

shubham1
Calcite | Level 5

hello 

 

But when I am running same code on SAS Host which I think is using SAS 9.2 there I am getting the , in the result 

 

Is this thing works on SAS 9.2 but not on SAS 9.4 ?

 

 

Kurt_Bremser
Super User

From what I take from the CATX section in https://www.lexjansen.com/scsug/2016/SCSUG_2016_Horstman_string_concatenation.pdf, the behavior of CATX has been the same in 9.2. It might be that the variable used in your mainframe SAS code is not really empty, but has a non-displayable character.

Shmuel
Garnet | Level 18

You had posted a comma preceding the abc in your original post.

Check again.

shubham1
Calcite | Level 5

Yes because I am using the function like this

zz=catx(',',x,z);

where x=, and Z='abc'

gamotte
Rhodochrosite | Level 12

There is no difference in this regard in the SAS 9.2 documentation :

https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002257076.htm

 

Check that it is indeed the same code that is run on the other host.

ErikLund_Jensen
Rhodochrosite | Level 12

Hi @shubham1 

 

CATX ignores arguments with missing values. It takes a list of variables and put a delimiter between <something> and <something>. So it never returns a string with a leading delimiter, a trailing delimiter or 2 or more consecutive delimiters. If you want delimiters between all elements, missing or not, I don't think there is a simple function. But it can be done with an array, see the following code:

 

data new;
    a=' ';
    b = 'abc';
    c=' ';
    d=' ';
    e = 'efg';
    f=' ';
run;

* No delimiters between missing;
data _null;
    set new;
    zz=catx(',',a,b,c,d,e,f);
    put zz;
run;

/* Result:
abc,efg
*/

* Delimiters between all variables;
data _null_;
    set new;
    array elements[*] _character_;
    length zz $20.;
    zz = elements{1};
    do i = 2 to dim(elements);
        zz = trim(zz) || ',' || trim(elements{i});
    end;
    put zz=;
run;

/* Result:
,abc,,,efg,
*/

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 9 replies
  • 1458 views
  • 1 like
  • 5 in conversation