Substitute characters

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

Substitute characters

data personal;

set learn.personal(drop=Food1-Food8);

substr(SS,1,7) = '******';    Question1: If I just replace the first 3 digits, the 5thand 6th digit with* (keep the dash between them), what code should I use?
substr(AcctNum,5,1) = '-';      Question 2: why substr(AcctNum,-1,1)='-'; doesn't work here?

run;

The data set looks like this:

Obs    SS           Gender  AcctNum   DOB

1123-45-6789M0192M11/15/1949
2013-54-9388F9981S01/02/1981
3112-11-1309M1322M03/29/1988
4778-44-4655F9899M07/04/1981
5445-45-4455M2938S08/09/1977

Thank you guys in advance!!!


Accepted Solutions
Solution
‎09-16-2014 11:28 PM
Trusted Advisor
Posts: 1,203

Re: Substitute characters

I think you are trying to do something like this

data want;

set have;

substr(ss,1,3)='***';

substr(ss,5,2)='**';

substr(acctnum,length(acctnum),1)='-';

run;

View solution in original post


All Replies
Solution
‎09-16-2014 11:28 PM
Trusted Advisor
Posts: 1,203

Re: Substitute characters

I think you are trying to do something like this

data want;

set have;

substr(ss,1,3)='***';

substr(ss,5,2)='**';

substr(acctnum,length(acctnum),1)='-';

run;

Super User
Super User
Posts: 6,318

Re: Substitute characters

You could also think of the problem a little differently.  Instead of what part of the value you want to replace think about what part you want to keep.

data have ;

infile cards dsd dlm='|';

length SS $11 Gender $1 AcctNum $5 DOB 8;

informat dob mmddyy10.;

format dob yymmdd10.;

input SS -- DOB;

cards;

123-45-6789|M|0192M|11/15/1949

013-54-9388|F|9981S|01/02/1981

112-11-1309|M|1322M|03/29/1988

778-44-4655|F|9899M|07/04/1981

445-45-4455|M|2938S|08/09/1977

;;;;

data want ;

  set have ;

  put (ss Acctnum) (=) @ ;

  ss = '***-**-' || substr(ss,8);

  AcctNum = substr(AcctNum,1,4) || '-';

  put '-> ' (ss Acctnum) (=) @ ;

run;


SS=123-45-6789 AcctNum=0192M -> SS=***-**-6789 AcctNum=0192-

SS=013-54-9388 AcctNum=9981S -> SS=***-**-9388 AcctNum=9981-

SS=112-11-1309 AcctNum=1322M -> SS=***-**-1309 AcctNum=1322-

SS=778-44-4655 AcctNum=9899M -> SS=***-**-4655 AcctNum=9899-

SS=445-45-4455 AcctNum=2938S -> SS=***-**-4455 AcctNum=2938-

Contributor
Posts: 68

Re: Substitute characters

A very interesting and different way of thinking.

Thank you Tom.

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 188 views
  • 4 likes
  • 3 in conversation