DATA Step, Macro, Functions and more

Strip portion of variable

Reply
Regular Contributor
Posts: 180

Strip portion of variable

data test(keep=Disaster);

set test1;

run;

 

Sample Output

Disaster

Hurricane Harvey
Hurricane Beula
Hurricane Agnes 
Wildfires

 

If the variable begins with Hurricane  I want to delete it and just show the word after the blank space.  I looked at compress, trim, strip functions however no examples.  How can I accomplish this ??     

Super User
Posts: 2,049

Re: Strip portion of variable

[ Edited ]

data have;
input disaster $20.;
datalines;
Hurricane Harvey
Hurricane Beula
Hurricane Agnes
Wildfires
;

data want;
set have;
if scan(disaster,1)='Hurricane' then _disaster=scan(disaster,2);
else _disaster=disaster;
run;

 

or with IFC function:

data want;
set have;
/*if scan(disaster,1)='Hurricane' then _disaster=scan(disaster,2);*/
/*else _disaster=disaster;*/
_disaster=ifc(scan(disaster,1)='Hurricane' , scan(disaster,2), disaster);
run;

Super User
Posts: 6,921

Re: Strip portion of variable

Posted in reply to novinosrin

I guess hurricanes usually receive one-word names.  Just in case there is more than one word following "Hurricane" this might be a little safer:

 

data want;
set have;
if upcase(scan(disaster,1))='HURRICANE' then _disaster=substr(disaster,11);
else _disaster=disaster;
run;

Super User
Posts: 2,499

Re: Strip portion of variable

Posted in reply to Astounding

or

data WANT;
  set HAVE;
  if upcase(DISASTER) =: 'HURRICANE ' then DISASTER = substr(DISASTER,11);
run;
Ask a Question
Discussion stats
  • 3 replies
  • 130 views
  • 0 likes
  • 4 in conversation