user1481397
user1481397

Reputation: 433

SAS, combine strings

I have a dataset test1, I want to generate a key which is the combination of any of the specified variables. For example, the key in ideal_1, or the key in ideal_2. I need to write a macro for this, but the challenges for me is that the number of the vars are not fixed, as you can see in ideal1, it is the combination of 2, and in ideal3 it is the combination of 3.

data test1;
input var1$ var2$ var3$ var4$ var5$ var6$;
datalines;

1 a  a b e
2 a  f b e
3 a  a a a
1 b  a a a
2 a  f b e

;
run;

data ideal_1;
 set test1;
 key=strip(var1)||strip(var2);
run;

data ideal_2;
 set test1;
 key=strip(var1)||strip(var2)||strip(var5);
run;

Upvotes: 1

Views: 405

Answers (2)

Tom
Tom

Reputation: 51621

Just use a variable list. You could store the list into a macro variable to make it easier to edit.

%let keylist=var1 var2 var5 ;

Then you can use the macro variable where ever you need it.

data ideal_2;
  set test1;
  key=cats(of &keylist);
run;

Upvotes: 1

Reeza
Reeza

Reputation: 21294

If the variables have a naming convention as in your example you can use something like the following, which uses the colon operator to concatenate all of the variables that start with the prefix VAR.

key = catt(of var:);

Upvotes: 0

Related Questions