DATA Step, Macro, Functions and more

how to join observations into one string?

Posts: 30

how to join observations into one string?

Hi all,

I have a dataset like this (obviously i have more than 4 rows though),

1  abc

2  def

3  ghi

4  jkl

I want to turn it into

1  abcdefghijkl


I've tried using a put function but for some reason i end up with 

abc           .

def            .

ghi            .

jkl             .


This is still a string, but just each row is now a paragraph instead, with a weird dot to show the row's end. I would like to remove that and just have the content of the variable from each row to concatenate and become one whole string. Also the data is alphanumeric as it wouldve been A-Z1-100. 

If anyone has any idea please help.

Posts: 260

Re: how to join observations into one string?

Posted in reply to AlexMoreton

I would use CALL CATS, which appends the second parameter (stripped of blanks) to the first variable:

data want;
  set have;
  length longstring $5000;
  retain longstring;
  call cats(longstring,string);
Posts: 20

Re: how to join observations into one string?

s_lassen has the right approach.  To get the first id you can simply set a variable with the value and retain it.


data d;
	length a 8 b $3;
	infile cards;
	input a b $;
1  abc
2  def
3  ghi
4  jkl

data b;
	length n1 8 n2 $32760;
	set d end=eof;
	retain n1 n2;
	if _n_ = 1 then n1 = a;
	call cats(n2,b);
	keep n1 n2;
	if eof then output;
Cameron | Selerity
Ask a Question
Discussion stats
  • 2 replies
  • 1 like
  • 3 in conversation