Combining mutiple char vars into few char vars of specified length

Accepted Solution Solved
Reply
Contributor
Posts: 66
Accepted Solution

Combining mutiple char vars into few char vars of specified length

Hi All,

I have some kind of requirement as below..hope some one can help on this.

I have consumers name field with character length 80. that i have splited in to multiple columns based on spaces..and now i have got around 15(but this count is not fixed) columns after spliting.

now, my requirement is I need to fit all the splited columns(that were created above with different lengths) values in to 5 output columns of length 26 each.

Can you please suggest and let me know if you need more inputs.

Thanks in advance.


Accepted Solutions
Solution
‎08-25-2014 08:17 AM
Super Contributor
Posts: 308

Re: Combining mutiple char vars into few char vars of specified length

Posted in reply to maheshtalla

Hello,

Here is one solution:

data have;
input (var1-var8) (:$15.);
datalines;
RAJINDERSAB AGGARWAL ANILSA REJAGGARWAL SANDHYA AGGARWAL NEETA AGGARWAL
;
run;

data want(drop= i j);
set have;
array allvar{8} var1-var8;
array newvars{5} $26 w1-w5 ;
j=1;
do i=1 to hbound(allvar);
if  ((length(strip(newvars{j}))+ length(trim(""))+length(strip(allvar{i} ))) le 26 ) then do;
  newvars{j}=strip(newvars{j}) || " " || allvar{i}  ;
end;
else do ;
  j=sum(j,1);
  newvars{j}=allvar{i};
  end;
end;
run;

View solution in original post


All Replies
Contributor
Posts: 62

Re: Combining mutiple char vars into few char vars of specified length

Posted in reply to maheshtalla

Hope the below code works..

For example  input file has below data. (use column format to read the data )  Or try using CATX or CATS or Cat function to concatenate with keep variable with lenght or format statement.

Mahesh    suresh     amr        kumar      christi  

data test;

length name1 name2 name3 name4 name5 $10.;

  input @01 name1 $10.

          @11 name2 $10.

          @21 name3 $10.

         @31 name4 $10.

          @41 name5 $10.

          ;

run;

Contributor
Posts: 66

Re: Combining mutiple char vars into few char vars of specified length

hi,

that is not my requirement. for example

i have the value RAJINDER AGGARWAL ANIL AGGARWAL SANDHYA AGGARWAL NEETA AGGARWAL.

I splited this value based on space and it created 8 different varables of different length. like this in my data i have different values so created 'n' noof variables.these varables i need to fit into 5 o/p varables of constant length 26.

my output should look as below

Contributor
Posts: 66

Re: Combining mutiple char vars into few char vars of specified length

Posted in reply to maheshtalla

o/p_1 = RAJINDER AGGARWAL ANIL it has to stop here since if we concatenate 4th varable it becomes >26 length and it may lead to truncate value. and we should not split the value so 4th var value has to go to next o/p var.

o/p_2 = AGGARWAL SANDHYA AGGARWAL

o/p_3 = NEETA AGGARWAL.

o/p_4 and o/p_5 should be null..

Respected Advisor
Posts: 4,173

Re: Combining mutiple char vars into few char vars of specified length

Posted in reply to maheshtalla

As long a none of the names has more than 26 characters below code should work:

data sample(drop=_Smiley Happy;

  retain _prxid;

  if _n_=1 then _prxid=prxparse('/(.{1,26})( |$)/oi');

  infile datalines truncover;

  input in_string $80.;

  array col {5} $26.;

  _start=1;

  _stop=-1;

  do _i=1 to dim(col);

    call prxnext(_prxid, _start, _stop, in_string, _position, _length);

    if _i>1 and _position=0 then leave;

    col[_i]=substrn(in_string,_position,min(_length,26));

    /*output;*/

  end;

  datalines;

RAJINDER AGGARWAL ANIL AGGARWAL SANDHYA AGGARWAL NEETA AGGARWAL

;

run;

Contributor
Posts: 62

Re: Combining mutiple char vars into few char vars of specified length

if it is for only 5 vars then u can use find function for example

check1=find(substr(x,1,26),' ','i',-27);

var1=substr(x,1,check1);

startvar=length(var1)+1;

check2=find(substr(x,startvar,26),' ','i',-27); ......

hope rest of the code u can try.....

Thank You

Solution
‎08-25-2014 08:17 AM
Super Contributor
Posts: 308

Re: Combining mutiple char vars into few char vars of specified length

Posted in reply to maheshtalla

Hello,

Here is one solution:

data have;
input (var1-var8) (:$15.);
datalines;
RAJINDERSAB AGGARWAL ANILSA REJAGGARWAL SANDHYA AGGARWAL NEETA AGGARWAL
;
run;

data want(drop= i j);
set have;
array allvar{8} var1-var8;
array newvars{5} $26 w1-w5 ;
j=1;
do i=1 to hbound(allvar);
if  ((length(strip(newvars{j}))+ length(trim(""))+length(strip(allvar{i} ))) le 26 ) then do;
  newvars{j}=strip(newvars{j}) || " " || allvar{i}  ;
end;
else do ;
  j=sum(j,1);
  newvars{j}=allvar{i};
  end;
end;
run;

Super User
Posts: 10,018

Re: Combining mutiple char vars into few char vars of specified length

Posted in reply to maheshtalla

It would be better if you could post an example .

data sample;
input (var1-var8) (: $20.);
  datalines;
RAJINDER AGGARWAL ANIL AGGARWAL SANDHYA AGGARWAL NEETA AGGARWAL
;
run;
data want(drop=temp lag_temp n i);
 set sample;
 array x{5} $ 26;
 array v{*} $ var: ;
 length temp lag_temp $ 200;
 n=0;
 do i=1 to dim(v);
  temp=catx(' ',temp,v{i});
  if length(temp) gt 26 then do; n+1;x{n}=lag_temp;temp=v{i};end;
  lag_temp=temp;
 end;
 n+1;x{n}=lag_temp;
run;

Xia Keshan

Contributor
Posts: 66

Re: Combining mutiple char vars into few char vars of specified length

Posted in reply to maheshtalla

Hi all,

Thanks a lot for your prompt responses. Solutions provided were really helpful and the results were as expected.

I once again Thanks to each one of you.

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 379 views
  • 9 likes
  • 5 in conversation