Concatenate character variables with CATX

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Concatenate character variables with CATX

Hi,

I'm stuck while using CATX function.

I have thirty string variable(COL1 to COL30) and i want to join this all variable separeated by comma by using CATX. It doesnt working.

Is there is any other way to do this.

Your little help may be great help.


Accepted Solutions
Solution
‎02-23-2017 07:56 AM
Super Contributor
Posts: 282

Re: Concatenate character variables with CATX

[ Edited ]

Hi,

 

Have you tried something like:

 

data _null_;
  col1='column 1';
  col2='column 2';
  col3='column 3';
  result=catx(',',of col1-col3);
  put result=;
run;

which for me gives:

 

result=column 1,column 2,column 3

 

If this does not work for you please post your code, what you expect and the log showing the error.

 

Also from @Bruno_SAS:

 You can make use of variable lists in the CATX function, see also sample below using an array:

 

data want;
  length colString $ 120; 
  array myVars{30} $ 4 col1 - col30;
  nChar = rank("A");
  do i = 1 to dim(myVars);
    myVars{i} = byte(nChar);
    nChar + 1; 
  end; 

  colString = catx(",", OF myVars{*});
run;

View solution in original post


All Replies
Solution
‎02-23-2017 07:56 AM
Super Contributor
Posts: 282

Re: Concatenate character variables with CATX

[ Edited ]

Hi,

 

Have you tried something like:

 

data _null_;
  col1='column 1';
  col2='column 2';
  col3='column 3';
  result=catx(',',of col1-col3);
  put result=;
run;

which for me gives:

 

result=column 1,column 2,column 3

 

If this does not work for you please post your code, what you expect and the log showing the error.

 

Also from @Bruno_SAS:

 You can make use of variable lists in the CATX function, see also sample below using an array:

 

data want;
  length colString $ 120; 
  array myVars{30} $ 4 col1 - col30;
  nChar = rank("A");
  do i = 1 to dim(myVars);
    myVars{i} = byte(nChar);
    nChar + 1; 
  end; 

  colString = catx(",", OF myVars{*});
run;
New Contributor
Posts: 4

Re: Concatenate character variables with CATX

[ Edited ]

Hi Amir,

 

I have data like this with missing values. variables are character.

Col1 col2 col3 col4….col28

3 . 7 8….25

2 6 . 11….28

;

 

I want

Col1 col2 col3 col4….col28 result

3 4 7 8….25  3,4,7,8,…,25

2 6 9 11….28 2,6,9,11,….28

 

I have written:

data d3;
   length result $ 50;
   SP=',';
   result =catx(SP, of col1-col28);
run;

 

LOG is:

 

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      19:16   19:21  
NOTE: Invalid numeric data, COL5='           1*           6' , at line 19 column 21.

Hope this clear.

SAS Super FREQ
Posts: 676

Re: Concatenate character variables with CATX

the Note

NOTE: Character values have been converted to numeric values at the places given by:

      (Line)Smiley SadColumn).

      19:16   19:21  

NOTE: Invalid numeric data, COL5='           1*           6' , at line 19 column 21.

Indicates that char values have been converted to numeric values, they are not coming from the CATX function

Super Contributor
Posts: 282

Re: Concatenate character variables with CATX

Hi,

Thanks for posting what you have. You shown some missing data, but you haven't said what result you want if the data is missing.

The data step does not seem complete as there is no input data specified, either through a set statement, or infile statement, or by some other means.

One of the log messages:

NOTE: Invalid numeric data, COL5='           1*           6' , at line 19 column 21.

shows data "1*           6'", are you expecting this kind of data too? Are you trying to perform maths on the character data?

I think we still need to be shown a clearer picture of what you have. Try posting a sample data step which we can run that shows what the problem is.

Regards,

Amir.

New Contributor
Posts: 4

Re: Concatenate character variables with CATX

Hi Bruno and Amir,

Its working. Thanks for your valuable suggestions and time.

SAS Super FREQ
Posts: 676

Re: Concatenate character variables with CATX

[ Edited ]

Hi as Amir mentioned you can make use of variable lists in the CATX function, see also sample below using an array:

 

data want;
  length colString $ 120; 
  array myVars{30} $ 4 col1 - col30;
  nChar = rank("A");
  do i = 1 to dim(myVars);
    myVars{i} = byte(nChar);
    nChar + 1; 
  end; 

  colString = catx(",", OF myVars{*});
run;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 17651 views
  • 2 likes
  • 3 in conversation