Help using Base SAS procedures

Removing string - in the data

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Removing string - in the data

hi i have the following data        

CRit

-currrent or prior tre

-prior use of treat

i am trying to get rid of  '-'  hypen  at starting of each value.

could any one please help me out to remove that.


Accepted Solutions
Solution
‎04-12-2013 09:45 AM
Super Contributor
Posts: 543

Re: Removing string - in the data

Sure

data test;

    string = "-minus";

   new_string = translate(string,"","-");

run;

.

View solution in original post


All Replies
Solution
‎04-12-2013 09:45 AM
Super Contributor
Posts: 543

Re: Removing string - in the data

Sure

data test;

    string = "-minus";

   new_string = translate(string,"","-");

run;

.

Occasional Contributor
Posts: 9

Re: Removing string - in the data

Thanks Tilea. That worked perfectly.

Occasional Contributor
Posts: 9

Re: Removing string - in the data

However it worked but in my values there are '-' in middle too those are removing too.

so i just need to remove only the first position of value only For '-'.

Super Contributor
Posts: 1,636

Re: Removing string - in the data

data have;

input v$;

cards;

abc

-bc

a-b

;

data want;

  set have;

  v=ifc(first(v)='-',compress(v,'-'),v);

proc print;run;

Super Contributor
Posts: 543

Re: Removing string - in the data

, very nice;

is the 'ifc" saying if the first character in string V is "-"??

That's awesome!

Smiley Happy

Or am I misunderstanding?

Yes, I am misunderstanding...I read on the IFC function.

is the first(v), the FIRST function that tells SAS that if the first character is "-' then so on...

still neat!

Super Contributor
Posts: 1,636

Re: Removing string - in the data

You are right.

It is the same as

if first(v)='-' then v=compress(v,'-');

Occasional Contributor
Posts: 9

Re: Removing string - in the data

Thanks Lin lin. but when I have "-" in the middle its getting removed too. For instance in the following example for the third value "-a-b" have "-" at the begining and the middle also.

data have;

input v$;

cards;

abc

-bc

-a-b

;

When I tried the following code,

if first(v)='-' then v=compress(v,'-');


My output is :

abc

bc

ab

but the required output is (i.e If there is any "_" in the middle other than first position I need to keep it as is)

abc

bc

a-b

Please help.

Super Contributor
Posts: 1,636

Re: Removing string - in the data

sorry!

try this one.

data have;
input v$;
cards;
abcc
-bcc
-a-b
;
data want;
  set have;
  v=ifc(first(v)='-',substr(v,2),v);
  proc print;run;
                                           Obs     v

                                            1     abcc
                                            2     bcc
                                            3     a-b


Super Contributor
Posts: 543

Re: Removing string - in the data

How about this:

data have;

input v$;

cards;

abc

-bc

-a-b

;

data want;

  set have;

  v1= prxchange("s/-//i",1,strip(v));

run;

Occasional Contributor
Posts: 9

Re: Removing string - in the data

Hi Linlin and Tilea,

Thank both of you for your help. I really appreciate your input.

Could you please help me with the following one too.

Data sample:

Set sample;

Amd1_9-;

AMD1_10-;

Run;

AMD1_9-History of another malignancy within the previous 5 years other than curatively treated non-

melanomatous skin cancer

AMD1_10-Current or prior treatment with estrogens and/or drugs with anti-androgenic properties such

as spironolactone > 50mg/kg, or progestational agents for the treatment of prostate cancer less than 6

months prior to randomization .

In my variable sample I have two sets of values one is 7 char in length second set is 8 char in length

I need to just remove up to colon part and my variable should be starting from alphabets.

Can u Please help me out.

Super Contributor
Posts: 543

Re: Removing string - in the data

Which Colon part?

What do you need?

This is what you have for a variable named sample it takes values amd1_9- and amd1_10-

Sample

Amd1_9-

AMD1_10-

And you want an new variable new_sample to take what values exactly:

New_Sample

?

?

Super Contributor
Posts: 543

Re: Removing string - in the data

You have a variable called sample like this

Sample

Amd1_9-

AMD1_10-

Could you please be more specific what do you need the new variable to look like:

Sample

?

?

And for the other problem with removing the string, why do you need more ways?

what's wrong with what either I or LinLin offered?

data have;

input string$;

cards;

abc

-bc

-a-b

;

data want;

  set have;

  string1= prxchange("s/-//i",1,strip(string));

run;

should work....

Super User
Posts: 1,070

Re: Removing string - in the data

Hi,

Please try to use compress(variable,'-'); check the sample code

data want;

     set have;

     newvar=compress(variable,'-');

run;

This code will remove the '-' hyphen from the variable.

Thanks,

Jagadish

Thanks,
Jag
Super Contributor
Posts: 358

Re: Removing string - in the data

How about

if string =: '-' then string = substr(string,2);

won't remove anything except a "-" from the first position.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 23 replies
  • 268 views
  • 9 likes
  • 5 in conversation