Help using Base SAS procedures

A Question on scan / or some function to

Reply
Contributor
Posts: 44

A Question on scan / or some function to

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.

New Contributor
Posts: 3

Re: A Question on scan / or some function to

Posted in reply to need_sas_help

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.


Contributor
Posts: 44

Re: A Question on scan / or some function to

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

New Contributor
Posts: 3

Re: A Question on scan / or some function to

Posted in reply to need_sas_help

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

Contributor
Posts: 44

Re: A Question on scan / or some function to

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.

Super User
Posts: 19,789

Re: A Question on scan / or some function to

Posted in reply to need_sas_help

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;

Regular Contributor
Posts: 180

Re: A Question on scan / or some function to

Posted in reply to need_sas_help

Try this:

data want(keep=comment suSmiley Happy;

  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

Contributor
Posts: 27

Re: A Question on scan / or some function to

Posted in reply to need_sas_help

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;

Super User
Posts: 19,789

Re: A Question on scan / or some function to

Posted in reply to bharathtuppad

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;

Ask a Question
Discussion stats
  • 8 replies
  • 230 views
  • 0 likes
  • 5 in conversation