Concatenation variables

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

Concatenation variables

Hai

  I have a doubt in data set programming

Here i can show my dataset.

IDTest1_Test2_Test3_test
U1Ta
U1Tb

U1Tc
U2Bi
U2Bj
U3Ck
U3Cl
U3Cm
U3Cn

I would like to become the Varible 4_Test as shown in below

ID4_test
U1T
U1a
U1b
U1c
U2B
U2i
U3j
U3C
U3k
U3l
U3m
U3n

Please Help me :smileyplain:

Regards

Krishna


Accepted Solutions
Solution
‎01-20-2015 04:06 PM
Super User
Posts: 19,780

Re: Concatenation variables

Posted in reply to KrishnaChandra

This is tested based on the data you provided. Mistakes in original code include a bug in array declaration (character and missing brace) and extra output statement.

data have;

infile datalines missover;

length ID Test Test1-Test3 $8;

input ID Test Test1-test3;

datalines;

U1 T a

U1 T b

U1 T c

U2 B . i

U2 B . j

U3 C . . k

U3 C . . l

U3 C . . m

U3 C . . n

;

data want;

set have;

by ID ;

array t_(3) $ test1-test3;

test4=test;

if first.id then output;

do i=1 to 3;

test4=t_(i);

if not missing(test4) then output;

end;

keep ID test4;

run;

View solution in original post


All Replies
Super User
Posts: 19,780

Re: Concatenation variables

Posted in reply to KrishnaChandra

Have you tried a transpose?

Otherwise a data step is a good way, using an output statement to control the output.

data want;

set have;

array t_3) test1-test3;

test4=test;

output;

do i=1 to 3;

test4=t_(i);

output;

end;

keep ID test4;

run;

Respected Advisor
Posts: 4,920

Re: Concatenation variables

Posted in reply to KrishnaChandra

You will have to use variable names starting with alphabetic characters (1_test is not a valid name, change it to test_1), then expand your dataset with:

data myDataset;

infile datalines missover;

length ID Test Test_1-Test_3 $8;

input ID Test Test_1 Test_2 Test_3;

datalines;

U1 T a

U1 T b

U1 T c

U2 B . i

U2 B . j

U3 C . . k

U3 C . . l

U3 C . . m

U3 C . . n

;

data want;

set myDataset; by test notsorted;

array t Test_:;

if first.test then output;

do i = 1 to dim(t);

    test = t{i};

    if not missing(test) then output;

    end;

keep ID test;

run;

proc print data=want noobs; run;

PG

PG
Frequent Contributor
Posts: 89

Re: Concatenation variables

Hai PG,

   Thanx.. But the test value T,B,C are  repeats ,then how it possible to out the observation(first.Test)???

Respected Advisor
Posts: 4,920

Re: Concatenation variables

Posted in reply to KrishnaChandra

Try replacing

set myDataset; by test notsorted;


by

set myDataset; by ID test notsorted;


That will output one observation for each consecutive ID with the same test.

PG

PG
Contributor
Posts: 53

Re: Concatenation variables

Also not missing(test) has to change since they are character values. try if trim(left(compress(test))) ne ''

Respected Advisor
Posts: 4,920

Re: Concatenation variables

Posted in reply to PoornimaRavishankar

The MISSING function works both for numeric and character arguments. The function returns true for empty strings. - PG

PG
Frequent Contributor
Posts: 89

Re: Concatenation variables

but first.test then output;  The code doesn't work properly.The test value not shown .i meant(T B C)


Solution
‎01-20-2015 04:06 PM
Super User
Posts: 19,780

Re: Concatenation variables

Posted in reply to KrishnaChandra

This is tested based on the data you provided. Mistakes in original code include a bug in array declaration (character and missing brace) and extra output statement.

data have;

infile datalines missover;

length ID Test Test1-Test3 $8;

input ID Test Test1-test3;

datalines;

U1 T a

U1 T b

U1 T c

U2 B . i

U2 B . j

U3 C . . k

U3 C . . l

U3 C . . m

U3 C . . n

;

data want;

set have;

by ID ;

array t_(3) $ test1-test3;

test4=test;

if first.id then output;

do i=1 to 3;

test4=t_(i);

if not missing(test4) then output;

end;

keep ID test4;

run;

Frequent Contributor
Posts: 89

Re: Concatenation variables

Thanx Reeza Smiley Happy

Super User
Posts: 19,780

Re: Concatenation variables

Posted in reply to KrishnaChandra

data want;

set have;

by ID ;

array t_3) test1-test3;

test4=test;

if first.id then output;

do i=1 to 3;

test4=t_(i);

if not missing(test4) then output;

output;

end;

keep ID test4;

run;

Frequent Contributor
Posts: 89

Re: Concatenation variables

Thanx PG Smiley Happy

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 319 views
  • 4 likes
  • 4 in conversation