DATA Step, Macro, Functions and more

Splitting a character value into several variables

Reply
Super Contributor
Posts: 272

Splitting a character value into several variables

Dear,

I posted this question today morning. The subject that I gave is different than what I posted. That's why I am posting again. Sorry.

 

In my data the following value is present. My code gave the output i need for all obs except for this value. With my code the value is split into five variables rather than four. In the vendor data, the value is split into 4 variables. I analysed it. I found in the second variable('value2' in the output needed), the length reaches exactly 200. With my code it truncates before. Please help in my code. Thanks

 

value

The OOO re-trained the site staff on re-conoooting current/active suooocts under the updated main OOO at screening. The site staff printed out the updated OOO and included it in the sooject source documents with a note to use the OOO at the subject's next site visit. The site staff created a note-to-file and reported this doooation to the OOO. The site staff will obtain the subject's re-consent during their next visit for the Oooo 00-000 study. The OOO will verify the OOO has been completed at the next OOO. The OOO reminded the site staff to work with the subject at their next Oooo 00-000 visit to obtain the missing consent signature, otherwise the site staff should document/update their note to file in source to reflect in the inability to obtain the signature (update 00JON2000).

 

code

 

data one;

length temp $200;

set data;

n+1;
do i=1 to countw(value,'');
temp=scan(value,i,' ');len=length(temp)+1;output;
end;
run;

 

data two;
set one;
by n;
retain sum;
if first.n then sum=0;
sum+len;
if sum gt 200 then do;group+1;sum=len;end;
run;

 

data three;
length output $ 200;
do until(last.group);
set two;
by n group;
output=catx(' ',output,temp);
end;
run;

 

output needed;

value1                        

 

The OOO re-trained the site staff on re-consenting current/active subjects under the updated main OOO at screening. The site staff printed out the updated OOO and included it in the subject source      

 

value2

documents with a note to use the OOO at the subject's next site visit. The site staff created a note-to-file and reported this deviation to the OOO. The site staff will obtain the subject's re-consent

 

value3

during their next visit for the Oooo 00-000 study. The OOO will verify the OOO has been completed at the next OOO. The OOO reminded the site staff to work with the subject at their next Oooo 00-000

 

value4

 

visit to obtain the missing consent signature, otherwise the site staff should document/update their note to file in source to reflect in the inability to obtain the signature (update 00JON2000).

 

 

output getting;

 

 

value1

The OOO re-trained the site staff on re-consenting current/active subjects under the updated main OOO at screening. The site staff printed out the updated OOO and included it in the subject source

 

value2

documents with a note to use the OOO at the subject's next site visit. The site staff created a note-to-file and reported this deviation to the OOO. The site staff will obtain the subject's

 

value3

re-consent during their next visit for the Oooo 00-000 study. The OOO will verify the OOO has been completed at the next OOO. The CRA reminded the site staff to work with the subject at their next

 

value4

Oooo 00-000 visit to obtain the missing consent signature, otherwise the site staff should document/update their note to file in source to reflect in the inability to obtain the signature (update

 

 value5

21JUN2016).

Super User
Posts: 9,682

Re: Splitting a character value into several variables

Try this:

if sum gt 200 then do;group+1;sum=len;end;

--->

if sum gt 201 then do;group+1;sum=len;end;

Respected Advisor
Posts: 4,651

Re: Splitting a character value into several variables

The reason behind @Ksharp's solution is that when the sum is 201, the last character counted is a space, so the real length of the reassembled substring will be 200.

PG
Valued Guide
Posts: 797

Re: Splitting a character value into several variables

[ Edited ]

@knveraraju91

 

Regardless of the issue of a blank in position 200 vs 201, it appears you're doing a lot of work for a straightforward task.

 

Your strategy is to count words, and sum up their cumulative lengths (plus a blank per word) to know when words will be assigned to a new group.  Then, for each group, you concatenate those words into the new OUTPUT variable.

 

 

Consider this strategy:

 

  1. Use the COMPBL function to remove excess internal blanks (i.e. double blanks become single blanks) in VALUE. 

  2. Instead of counting words, why not start with a txt length (variable TXTLEN) of 200, beginning with character 1 (variable STARTCHR=1) and ending with character 200 and see if it is immediately followed by a blank (i.e. look at column 201=STARTCHR+TXTLEN for a blank).  If it's not a blank, reduce txt length by 1 and see if it is followed by a blank.  Once a blank has been found copy all the text from STARTCHR and length TXTLEN to value1. 

    Then for value2, increment STARTCHR by TXTLEN+1 (i.e. point at first unproceessed non-blank), look for blanks at position STARTCHR+200 and work backward until one is found.  Copy the text from the new STARTCHR for a length of the new TXTLEN to value2.

    Etc. Etc.

    If startchr finally get incremented beyond length of value, then no further work is needed - stop the loop.


This is a lot less programming than counting words and concatenating them 1 at a time. 

 

regards,

Mark


That strategy is in the loop below:


data want (drop=v startchr txtlen);

  array val{4} $200 value1-value4;
  set data;
  n+1;
  value=compbl(value);  *remove excess blanks *;
  startchr=1;
  do v=1 to dim(val) until (startchr>length(value));

/* Nothing inside this do loop since we only want it to stop when a blank is found*/ do txtlen=200 to 1 by -1 while(notspace(char(value,startchr+txtlen))); end;
/* copy from startchr for length txtlen to value1, value2, value3, value4*/ val{v}=substr(value,startchr,txtlen); startchr=startchr+txtlen+1; end; run;

 

Respected Advisor
Posts: 4,651

Re: Splitting a character value into several variables

Well done @mkeintz,

 

I would suggest moving the startchr=1 statement before the loop and replacing

 

while(substr(value,startchr+txtlen,1)^=' ')

 

with 

 

while(notspace(char(value, startchr + txtlen)))
PG
Valued Guide
Posts: 797

Re: Splitting a character value into several variables

Thx
Will do when I have a real keyboard.
Respected Advisor
Posts: 3,894

Re: Splitting a character value into several variables

[ Edited ]

Or based on @mkeintz reformulation of the problem here an approach using a RegEx.

data have(drop=_:);
  length source_var $800;
  do _char='a','b','c','d';
    do until(lengthn(source_var)>=620);
      source_var=catx(' ',source_var,repeat(_char,ceil(ranuni(1)*10)));
    end;
    output;
    call missing(source_var);
  end;
  stop;
run;

data want(drop=_:);
  array target_var (4) $200;
  retain _re;
  if _n_=1 then
    _re=prxparse('/^\s*(.{1,200})\s+(.{1,200})\s+(.{1,200})\s+(.{1,200})\s*$/o');

  set have;
  if prxmatch(_re, source_var) then
    do _i=1 to dim(target_var);
      target_var[_i]=prxposn(_re, _i, source_var);
    end;
run;

 

Ask a Question
Discussion stats
  • 6 replies
  • 221 views
  • 8 likes
  • 5 in conversation