BookmarkSubscribeRSS Feed
nickspencer
Obsidian | Level 7
Hello All,
I have a SAS dataset with account numbers(character). I want to display only last 4 masking the remaining with ‘x’.

Input:
Account
123456
34567898

Output:
Account
xx3456
xxxx7898

Any suggestions will be highly appreciated.
5 REPLIES 5
ballardw
Super User

What do you want if the length of the account is less than or equal to 4 characters?

Is the Account numeric or character?

 

If account is character valued and leave the account alone if there are 4 or fewer characters:

data have;
input Account :$10.;
datalines;
123456
34567898
1234
;

data want;
   set have;
   if length(account) ge 5 then 
       account = cats(repeat('x',length(account)-5),substr(account,length(account)-3));
run;

Please not the use of a data step to provide some working values. That way we don't have to ask questions like is the Account numeric or character.

CATS concatenates string values ignoring leading and trailing spaces, Repeat will create the same character a number of times look at the documentation as to why the -5 gets involved.Length returns the number characters excluding trailing spaces. Substr selects a number of characters from a given starting position in the string. If there is no length it uses from the given position to the end of the string. Again look at the values for why -3 is used instead of -4.

This is only one way.

andreas_lds
PROC Star

Assuming that account is a char variable, as it should be:

 

data want;
   set have;

   if lengthn(account) > 4 then do;
      new = cats(repeat('x', lengthn(account)-5), prxchange('s/.*(.{4})$/$1/', 1, trim(account)));
   end;
run;
LinusH
Tourmaline | Level 20

If this is/becomes a comon practice at your site, you might want to look into adding masking capabilities to your infrastructure.

On SAS9 this is managed in Federation Server or DQ/QKB.

There are also external databases that offers this in the data layer, wich is more flexible than hard rewrite of values.

Data never sleeps
RichardAD
Fluorite | Level 6

You can use a left-hand side SUBSTR to replace characters.

 

  accnom = accno;
  substr(accnom,1,length(accnom)-4) = repeat('*',length(accnom));

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 5 replies
  • 185 views
  • 2 likes
  • 6 in conversation