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

Hello

I am using CATX to concatenate multiple string fields with delimator minus between them.

In the resulted field I get value 100-200-300-.-.-.-.-.-.-.

However I want to concatenate only mon-missing fields  that the value will be 100-200-300

What is the way to correct it please?

 

Data have;
input X1 X2 X3 X4 X5 X6 X7 X X9 X10;
cards;
100 200 300 . . . . . . .
;
Run;

Data wanted;
set have;
Vector=CATX('-',OF X:);
Run;
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

CATX() id doing implicit conversion from number to text so "missing value" is set to a "dot",

Data have;
input X1 X2 X3 X4 X5 X6 X7 X X9 X10;
cards;
100 200 300 . . . . . . .
;
Run;

Data wanted;
set have;
array XX X:;
length Vector $ 100;
do over XX;
  if XX then Vector=CATX('-',Vector, XX);
end;
Run;
proc print;
run;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

6 REPLIES 6
yabwon
Onyx | Level 15

CATX() id doing implicit conversion from number to text so "missing value" is set to a "dot",

Data have;
input X1 X2 X3 X4 X5 X6 X7 X X9 X10;
cards;
100 200 300 . . . . . . .
;
Run;

Data wanted;
set have;
array XX X:;
length Vector $ 100;
do over XX;
  if XX then Vector=CATX('-',Vector, XX);
end;
Run;
proc print;
run;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ronein
Meteorite | Level 14
Thanks,
You wrote "if XX then Vector=CATX('-',Vector, XX);"
As I understand it is same to write "if XX ne . then Vector=CATX('-',Vector, XX);"
My question:
How SAS knows that IF XXX is like IF XXX ne . ???
yabwon
Onyx | Level 15

#maxim1 [https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers...]

Read the doc. 😉

 

"In SAS, any numeric value other than 0 or missing is true, and a value of 0 or missing is false."

 

https://documentation.sas.com/doc/en/lrcon/9.4/p00iah2thp63bmn1lt20esag14lh.htm#p084fxdgd12w4sn1o2xi...

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ksharp
Super User
options missing=' ';
Data wanted;
set have;
Vector=CATX('-',OF X:);
Run;
ballardw
Super User

Since your issue resolves around MISSING values consider the options involved with missing:

 

options missing=' ';
Data wanted;
set have;
Vector=CATX('-',OF X:);
Run;
options missing='.';

You get all the - in your result because, as correctly stated by @yabwon the implicit conversion to the dot character for missing numeric. Setting the Missing option to a blank means the conversion still happens but CATX will remove all trailing blanks, the only character, and get the desired result.

When using this approach don't forget to reset the missing option to avoid other possible confusions.

PaigeMiller
Diamond | Level 26

I think a better idea is to use arrays on X1-X10, rather than the much harder creating the proper string with missings '100-200-300' and then working with this string. Then its easy to test to see if X4 is missing and just skip processing in that case.

--
Paige Miller

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
  • 6 replies
  • 719 views
  • 5 likes
  • 5 in conversation