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

Hi All, 

 

I need help in masking a variable by retaining the special characters at the same position in the masked value.

 

For example :

 

Name :

Alice 

(Mr).Hellen 'O' Brain

Hau & Pin

 

Expected Result after masking:

Name:

STUDENT09876

(ST).UDENT1'2'345

STU&DENT09876

 

all names are to masked as STUDENT followed by their student id.

if the names have special characters , the special character is to be placed at the same position as in the original string. even if it has multiple special characters.

 

Any help for the multiple special character scenario is appreciated.

 

Thanks in Advance !!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

How about this?

 

data have;
input Name $ 1-21 ID $ 23-27;
datalines;
Alice                 09876
(Mr).Hellen 'O' Brain 12345
Hau & Pin             09876
;

data want(keep=Stid);
    set have;
    stid=cats('STUDENT', ID);
    do pos=1 to length(compress(Name));
        c=char(compress(Name), pos);
        if prxmatch('/[^A-Z 0-9]/i',c) then do;
            if pos=1 then stid=cats(c, stid);
            else stid = cat(substr(stid, 1, pos-1), c, substr(stid, pos));
        end;
    end;
run;

Result:

 

stid
STUDENT09876
(ST).UDENT1'2'345
STU&DENT09876

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

So the Student ID is given in another variable or?

somu19tec
Calcite | Level 5
Yes , student ID is another variable which we can concatenate.
PeterClemmensen
Tourmaline | Level 20

How about this?

 

data have;
input Name $ 1-21 ID $ 23-27;
datalines;
Alice                 09876
(Mr).Hellen 'O' Brain 12345
Hau & Pin             09876
;

data want(keep=Stid);
    set have;
    stid=cats('STUDENT', ID);
    do pos=1 to length(compress(Name));
        c=char(compress(Name), pos);
        if prxmatch('/[^A-Z 0-9]/i',c) then do;
            if pos=1 then stid=cats(c, stid);
            else stid = cat(substr(stid, 1, pos-1), c, substr(stid, pos));
        end;
    end;
run;

Result:

 

stid
STUDENT09876
(ST).UDENT1'2'345
STU&DENT09876
Ksharp
Super User
data have;
input Name $ 1-21 ID $ 23-27;
datalines;
Alice                 09876
(Mr).Hellen 'O' Brain 12345
Hau & Pin             09876
;

data want;
    set have;
	old_name=name;
	name=compress(name,,'s'); 
    stid=cats('STUDENT', ID);
n=0;
do i=1 to max(length(name),length(stid));
 t=char(name,i);
 if anyalpha(t) or missing(t) then do;
   n+1; substr(name,i,1)=char(stid,n);
 end;
end;
drop n i t;
run;
somu19tec
Calcite | Level 5

Thanks Ksharp, this is also the solution i was looking for. but  i am not able to mark this as a solution as i already marked another reply as solution.

ballardw
Super User

Since you have a student Id I really question the utility of this. It sort of smells like a programming class assignment.

somu19tec
Calcite | Level 5
Hi Ballardw,
It's not an assignment , we are working on a migration project for one of
our client which is a school , so inorder to mask the production data , we
are doing this.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2782 views
  • 0 likes
  • 4 in conversation