I Have a character variable (Comment) with the following value:
Comment
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Pitney : Cutting Device Failure : Cannot Insert Cutter; Pitney : Cutting Device Failure : Component Damaged; Houston : Cleaning Device Failure : Vibration
I need to create three charactert variables (SU1, SU2, SU3) from the varaible - Comment as every new varaible starts after first ":" and ends before ";". The next
variable starts after ";" and the first ":" and ends before ";". and goes on.....
The result dataset wither new variables should be as below:
SU1 SU2 SU3
------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------
Cutting Device Failure : Cannot Insert Cutter Cutting Device Failure : Component Damaged Cleaning Device Failure : Vibration
Thank you so much.
I believe
SU1 =Scan(Comment,1,';') ;
SU2 =Scan(Comment,2,';') ;
SU3 =Scan(Comment,3,';') ;
Of course, I have not checked the syntax completely, so some tweaking may be required.
Thanks a lot LionIT for you code. But it doesn't solve the main complexity I am facing:
charactert variables (SU1, SU2, SU3) from the varaible - Comment as every new varaible starts after first ":" and ends before ";". The next
variable starts after ";" and the first ":" and ends before ";".
Sorry about that...missed the 'Pitney' piece....or should I say 'Pitney' I didn't read the whole note!
SU1, SU2 and SU3 after the first piece of code would now contain the desired comments other than the extra 'Pitney :' at the start.
So, we need to substr starting at the position afer the ':' to the end of hte string. The end piece would be SU1 = Substr(SU1, startinglocationtobedetermined, length(SU1)-Startinglocationtobedetermined) ;
For startinglocationtobedetermined insert the following.
Indexc(SU1,':')+1
Thanks LionelT . Not famliar with the code you just provided. Not too clear too me. If you have a minute to spare would you be kind enough to provide the exact code? Sorry I have failed to follow this instruction. Please help me.
Here's two ways. The second relies on your fixed structure as in the example.
data have;
var="Pitney : Cutting Device Failure : Cannot Insert Cutter; Pitney : Cutting Device Failure : Component Damaged; Houston : Cleaning Device Failure : Vibration";
run;
data want;
set have;
*separate out into parts by semicolon;
temp1=scan(var, 1, ";");
temp2=scan(var, 2, ";");
temp3=scan(var, 3, ";");
*Substring remaining variable by finding the first colon delimiter;
var1=substr(temp1, find(temp1, ":")+2);
var2=substr(temp2, find(temp2, ":")+2);
var3=substr(temp3, find(temp3, ":")+2);
*keep var1-var3;
run;
*A different option by scanning into component parts and then concatenating them together;
data want2;
set have;
*separate out into parts by semicolon;
var1=catx(":", scan(var, 2, ";:"), scan(var, 3, ";:")) ;
var2=catx(":", scan(var, 5, ";:"), scan(var, 6, ";:")) ;
var3=catx(":", scan(var, 8, ";:"), scan(var, 9, ";:")) ;
keep var1-var3;
run;
Try this:
data want(keep=comment su:);
length comment test $ 200;
array su{3} $ 200;
comment="Pitney : Cutting Device Failure : Cannot Insert Cutter; Pitney : Cutting Device Failure : Component Damaged; Houston : Cleaning Device Failure : Vibration";
test=cats(comment,';');
l=length(test);
do n=1 to 3 ;
from=index(test,':');
if from=0 then leave;
to=index(test,';');
if to=0 then to=l;
su{n}=cats(substr(test,from+1,to-from-1));
test=substr(test,to+1);
l=l-to;
end;
run;
CTorres
data have;
infile cards truncover;
input comment $250. ;
datalines4;
:Cutting Device Failure : Cannot Insert Cutter;:Cutting Device Failure : Component Damaged;:Cleaning Device Failure : Vibration;
;;;;
proc print;
run;
data want;
set have;
su1=scan(comment,1,";");
su2=scan(comment,2,";");
su3=scan(comment, 3,";");
%macro abc;
%do i=1 %to 3;
su&i=substr(su&i,2);
%end;
%mend;
%abc;
run;
proc print;
run;
Why would you use macro looping here? Use an array instead.
data want;
set have;
arrray su(3) $ su1-su3;
do i=1 to 3;
su(i)=scan(comment,i,";");
su(i)=substr(su(i),2);
end;
run;
proc print;
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 the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.