stallingOne
stallingOne

Reputation: 4006

SAS How to concatenate strings several times in the same variable during a data step

A very basic question.

How come I cannot change a variable in a datastep like this?

data want;
   aaaaa='[';
   aaaaa=cats(aaaaa,'hello');
   aaaaa=cats(aaaaa,']');
   put aaaaa;
run;

aaaaa will be equal to '[' , I expect '[hello]'

I need to define aaaaa in multiple steps because I need to construct aaaaa with do loops (with unpredictable length), I can't define aaaaa in just one step.

Thanks!

Upvotes: 1

Views: 450

Answers (3)

Longfish
Longfish

Reputation: 7602

As per my comments, here's an alternative way using call cats.

data want;
length aaaaa $300;
call cats(aaaaa,'[');
call cats(aaaaa,'Hello');
call cats(aaaaa,']');
run;

Upvotes: 0

andrey_sz
andrey_sz

Reputation: 751

You can add attrib or format statement to data-step:

data want;
   attrib 
      aaaaa format=$200.;
   aaaaa='[';
   aaaaa=cats(aaaaa,'hello');
   aaaaa=cats(aaaaa,']');
   put aaaaa;
run;

Upvotes: 1

Jetzler
Jetzler

Reputation: 797

When first calling aaaaa SAS will assign a length to the variable. In this case length aaaaa $1 as you only assign one character. SAS data types are not dynamic. Start your datastep by assigning your variable a fixed length that covers the maximal expected length e.g.

data want;
    length aaaaa $300;
    aaaaa='[';
    aaaaa=cats(aaaaa,'hello');
    aaaaa=cats(aaaaa,']');
    put aaaaa;
run;

Upvotes: 3

Related Questions