BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
muralip2013
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
AncaTilea
Pyrite | Level 9

Sure

data test;

    string = "-minus";

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

run;

.

View solution in original post

23 REPLIES 23
AncaTilea
Pyrite | Level 9

Sure

data test;

    string = "-minus";

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

run;

.

muralip2013
Calcite | Level 5

Thanks Tilea. That worked perfectly.

muralip2013
Calcite | Level 5

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 '-'.

Linlin
Lapis Lazuli | Level 10

data have;

input v$;

cards;

abc

-bc

a-b

;

data want;

  set have;

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

proc print;run;

AncaTilea
Pyrite | Level 9

, 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!

Linlin
Lapis Lazuli | Level 10

You are right.

It is the same as

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

muralip2013
Calcite | Level 5

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.

Linlin
Lapis Lazuli | Level 10

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


AncaTilea
Pyrite | Level 9

How about this:

data have;

input v$;

cards;

abc

-bc

-a-b

;

data want;

  set have;

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

run;

muralip2013
Calcite | Level 5

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.

AncaTilea
Pyrite | Level 9

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

?

?

AncaTilea
Pyrite | Level 9

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

Jagadishkatam
Amethyst | Level 16

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
OS2Rules
Obsidian | Level 7

How about

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

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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