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).
Try this: if sum gt 200 then do;group+1;sum=len;end; ---> if sum gt 201 then do;group+1;sum=len;end;
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.
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:
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;
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)))
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.