Amit Kumar
Amit Kumar

Reputation: 3

Concatenation in SAS with name

Hi i have 25 variables like Name1, name2, name3.....name 25. Few names are having 0 data which means few name are have the value 0.

I want to concate all the name from 1 to 25 and drop those name which has 0 values.

i am trying

data test; set need; new_name = catx (',',Name1,Name2, Name3, Name4, Name5, Name6, Name7, Name8, Name9, Name10, Name11, Name12, Name13, Name14, Name15, Name16, Name17, Name18, Name19, Name20, Name21, Name22, Name23, Name24, Name25); ); run;

but i am getting name like 0,0,0,0,0,Amit,0,0,0,Dave,0,0,0,Pam,0,0,0,0,Deepka,0,0,0,0,0,0. However, i only need names not 0 value in the outcome. Variable data type is charracter

Any help would be much appreciated

Upvotes: 0

Views: 77

Answers (1)

Negdo
Negdo

Reputation: 532

This should work:

data have;
    input (Name1-Name25) (:$20.);
    datalines;
0 0 0 0 0 Amit 0 0 0 Dave 0 0 0 Pam 0 0 0 0 Deepka 0 0 0 0 0 0
Tom Steve 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Richard 0 0 0 0
;
run;

First I changed 0 with nulls, so catx doesn't concatenate those zeros.

Data have;
    set have;
    array Name[25];
    do i=1 to 25;
        if Name[i]='0' then do;
        Name[i]='';
        end;
    end;
run;

Your code then works just fine.

proc sql;
    create table want as
        select catx (',',Name1,Name2, Name3, Name4, Name5, Name6, Name7, Name8, Name9, Name10, Name11, Name12, Name13, Name14, Name15, Name16, Name17, Name18, Name19, Name20, Name21, Name22, Name23, Name24, Name25) as new_name
            from have
    ;
quit;

This is naturally not the only way to do this. Best way would probably be to fix the issue at the source - to put proper null values into the table instead of 0s.

Upvotes: 0

Related Questions