user3658367
user3658367

Reputation: 641

arrays in SAS with more columns

I have a dataset

Data have;
input A B C; 
cards; 
1 . .
. . 1
1 1 .
run;

And I am looking for an output which is like this.

A B C  OUT
1 . .  A
. . 1  C
1 1 . A,B

I wrote the program this way:

Data want;
set have; 
array U(3)A B C;
do i=1 to 3; 
if U(i)^=. then OUT=cat(vname(u(i),',');
end; 
run;

This gives only the last VNAME and not the concatenation.

Upvotes: 1

Views: 64

Answers (1)

Longfish
Longfish

Reputation: 7602

When using a separator with concatenation, then catx is the function to use, or even better call catx which negates the need to put out = and using out in the concatenation as well. Both these functions will trim any leading or trailing blanks.

The other problem with your code is that because out is derived from numeric variables, SAS will default the type to numeric as well. You need to define the type to character beforehand (I've done this with a length statement.

The following code achieves your goal.

Data have;
input A B C; 
cards; 
1 . .
. . 1
1 1 .
run;

data want;
set have;
length out $20;
array U{3} A B C;
do i = 1 to 3;
if not missing(U{i}) then call catx(',',out,vname(U{i}));
end;
drop i;
run;

Upvotes: 3

Related Questions