BookmarkSubscribeRSS Feed
need_sas_help
Calcite | Level 5

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.

8 REPLIES 8
LionelT
Calcite | Level 5

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.


need_sas_help
Calcite | Level 5

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 ";". 

LionelT
Calcite | Level 5

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

need_sas_help
Calcite | Level 5

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.

Reeza
Super User

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;

CTorres
Quartz | Level 8

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

bharathtuppad
Obsidian | Level 7

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;

Reeza
Super User

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1364 views
  • 0 likes
  • 5 in conversation