DATA Step, Macro, Functions and more

How to remove special characters only from first position?

Accepted Solution Solved
Reply
Super Contributor
Posts: 436
Accepted Solution

How to remove special characters only from first position?

I've a data like below. Here I need to remove only the colon (Smiley Happy in the variable 'userid' if it is a first character.

userid
:sassrv
:datapo@saspw
74064:datapo@saspw

I need the data as below.

userid
sassrv
datapo@saspw
74064:datapo@saspw

I tried with compress function to achieve this, but could not succeed. Please advise,


Accepted Solutions
Solution
‎08-21-2015 07:41 AM
Super User
Posts: 10,044

Re: How to remove special characters only from first position?

Code: Program

data have;
input userid $40.;
cards;
:sassrv
:datapo@saspw
74064:datapo@saspw
;
run;
data want;
set have;
new=prxchange('s/^://',1,userid);
run;

View solution in original post


All Replies
Super User
Posts: 5,437

Re: How to remove special characters only from first position?

Why didn't it work with compress?

You need to use it in conjunction with if, substr and cats.

Data never sleeps
Super Contributor
Posts: 436

Re: How to remove special characters only from first position?

Thanks for your response.

May I request to give a code snippet as I was doing something wrong with these function.

Solution
‎08-21-2015 07:41 AM
Super User
Posts: 10,044

Re: How to remove special characters only from first position?

Code: Program

data have;
input userid $40.;
cards;
:sassrv
:datapo@saspw
74064:datapo@saspw
;
run;
data want;
set have;
new=prxchange('s/^://',1,userid);
run;
Contributor
Posts: 65

Re: How to remove special characters only from first position?

data log_analysis;

infile '/usr/sas/sas_config/Lev1/SASApp/Logs/SASApp_STP_2015-08-19_tmp1_19142.log' truncover;

input var : $ 3000.;

var1 = _infile_;

if var1 = :'2015';

Date_With_TimeStamp = scan(var1,1," ");

Status = scan(var1,2," ");

Processid = scan(var1,3," ");

if substr(scan(var1,4," "),1,1)=':' then userid =substr(scan(var1,4," "),2) ;

else userid = scan(var1,4," ");

Details = scan(var1,-1,'-');

drop var var1;

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 253 views
  • 6 likes
  • 4 in conversation