Solved
Contributor
Posts: 68

# 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

1 2 3 4 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

Accepted Solutions
Solution
‎09-16-2014 11:28 PM
Posts: 1,270

## 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;

All Replies
Solution
‎09-16-2014 11:28 PM
Posts: 1,270

## 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
Posts: 8,127

## 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 and locked.