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 !!
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
So the Student ID is given in another variable or?
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
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;
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.
Since you have a student Id I really question the utility of this. It sort of smells like a programming class assignment.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.