concatenation of several string variables

Reply
Regular Contributor
Posts: 228

concatenation of several string variables

Hi there.

I'd like to ask how do I combine several string variables into one new created string variable?

Let say I have 3 string variables i.e.

strA="some statement A.";

strB="some statement B.";

strC="some statement C.";

and the new created one i.e str is the concatenation of those strA, strB and strC that each line of variable str will correspond to strA, strB and

strC.

I'd the resulted dataset to be like as below if there's only one subject i.e. id=1.    

id     var

1     -some statement a.

       -some statement b.

       -some statement c.


I've tried str=strA || "\n" || strB || "\n" || strC; as well as catx but they don't give me what I intend to produce.


Any help is greatly appreciated. Smiley Happy

Super User
Posts: 3,260

Re: concatenation of several string variables

A common problem with concatenation is contributing strings may have trailing or leading blanks. Try the CATS function which will remove these blanks while concatenating. If this doesn't help provide an example of your output so we can understand better what you are doing.

Regular Contributor
Posts: 228

Re: concatenation of several string variables

Thanks SASKiwi for your response.

I have used "strip" for strA, strB and strC before concatenating them.

i thought by specifying "\n" will place each strA, strB and strC all in a separate new line but no.

Super User
Posts: 3,260

Re: concatenation of several string variables

The way you assign a value to a string or variable and how you "print" it across multiple lines are two separate things. It would help if you could explain the background to what you want to do, including your actual code so we can give you better advice.

Regular Contributor
Posts: 228

Re: concatenation of several string variables

Thanks SASKiwi for your response again.

My apology for any unclarity.

I think what Tom suggested is something that I need for now.

Thanks again.

Super User
Super User
Posts: 7,076

Re: concatenation of several string variables

You can do it with PROC REPORT, at least to the listing output destination.

data x;

id=1;

strA="some statement A.";

strB="some statement B.";

strC="some statement C.";

str = catx('*',strA,strB,strC);

run;

proc report nofs nocenter headline data=x split='*';

  column id str;

  define id / order ;

  define str / width=30 flow ;

run;

         id  str

  -----------------------------------------

          1  some statement A.

             some statement B.

             some statement C.

Regular Contributor
Posts: 228

Re: concatenation of several string variables

Thanks Tom for your response.

I think this is something that I need and can help me to begin with.

Regular Contributor
Posts: 228

Re: concatenation of several string variables

Hi Tom.

When I ods the proc report output to Excel spreadsheet, the asterisk will show instead.

What should I do in order to get the same output as that produced by proc report?

Thanks in advance.

Super Contributor
Posts: 474

Re: concatenation of several string variables

Hello wong.

For ODS reporting you should work with the escape character feature.

data x;

id=1;

strA="some statement A.";

strB="some statement B.";

strC="some statement C.";

str = catx('^n',strA,strB,strC);

run;

ods html file='report.xls' stylesheet;

ods escapechar='^';

proc report nofs nocenter headline data=x split='*';

  column id str;

  define id / order ;

  define str / width=30 flow ;

run;

ods _all_ close;


More on this here:

http://support.sas.com/documentation/cdl/en/odsug/61723/HTML/default/viewer.htm#a002233270.htm

Cheers from Portugal.

Daniel Santos @ www.cgd.pt

Regular Contributor
Posts: 228

Re: concatenation of several string variables

Posted in reply to DanielSantos

Thanks Daniel!

Super User
Posts: 11,343

Re: concatenation of several string variables

A field in Excel with all asterisks usually means the value is too long to display within the default column width. Make the column wider and the values should appear.

SAS Employee
Posts: 1

Re: concatenation of several string variables

Try this:

data hello;

     length varname $ 1024.;

     varname = catx(' ', 'line', 'number', '1', '0a'x, 'line', 'number', '2');

run;

proc print data=hello;

run;

Ask a Question
Discussion stats
  • 11 replies
  • 1003 views
  • 9 likes
  • 6 in conversation