I have a requirement where the String has to be masked with the letter 'x'.
for. eg. Sreelekha should be 'xxxxxxxxx'.
I used the substr function to do this:
For a certain, column in a table where the length of the string is quite long, the string is getting replaced with just 1 X. ( x).Please refer screenshot.
Can you please help me with this issue.
Post example data (in a datastep, see https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...) and your code. Follow the advice in https://communities.sas.com/t5/help/faqpage/faq-category-id/posting?nobounce for posting code.
Please find the code being used below.
%macro macro_name(col_name);
slen = length (&col_name);
i=0;
do until( i > slen);
substr(&col_name,i,slen) = "x";
i=i+1;
end;
%mend;
data table_name (drop=slen);
set mast_table;
%macro_name(Column1);
%macro_name(Column2);
drop i;
run;
I am facing issue with Column2.
Works for me:
%macro macro_name(col_name);
slen = length(&col_name);
i = 0;
do until (i > slen);
substr(&col_name,i,slen) = "x";
i = i + 1;
end;
%mend;
data test;
x1 = 'Sreelekha ';
%macro_name(x1);
run;
proc print data=test noobs;
run;
Result:
x1 slen i xxxxxxxxx 9 10
If your results differ, post your example data as requested.
Note that I used the "little running man" icon to post code, as shown in the link I gave you. Please follow advice.
The main posting window changes code layout and makes copy/pasting difficult.
I will use that.
Apologies for missing out.
Can you please try the string " Abcd Efghijklmn , Schools and Board of Management ".
Look at the log. Your use of slen as the third parameter of the substr() function causes problems.
Since you just want to overwrite a string from start to last non-blank character, you can simplify your macro a lot:
%macro macro_name(col_name);
&col_name = repeat("x",length(&col_name));
%mend;
which also has the advantage that no additional variables are created.
PS the whole thing is now so simple that you will need a macro only when you want to reuse that code line a LOT. If you only need it for three variables, write the original code into the data step; makes for easier maintenance.
Hello Kurt,
Thanks a lot 🙂 This worked.
I need it for updating 20 tables, hence would be using it as a macro.
You have fallen into the trap of many SAS programmers, in your immediate need to jump into macro programming, Macro is not the programming language. Use Base SAS which is the programming language and you will find your coding far simpler:
data have; length a $10; a="abc345kl"; output; a="tyhg222"; output; a="aa"; output; run; data want; set have; a=prxchange("s/[^A-Z ]/X/",-1,a); run;
Thank you for the assistance.
I need to update 20 tables and hence many columns.
So. I would need a macro.
Thank you..!!
Is there some pattern to the datasets/varaibles, or just all datasets in one library and all character varaibles?
data _null_; set sashelp.vcolumns (where=(libname="WORK")); by memname; if first.memname then call execute(cats('data work.',memname,'; set work.',memname,';')); if type="char" then call execute(name,'=prxchange("s/[^A-Z ]/X/",-1,',name,');'); if last.memname then call execute(';run;'); run;
This will run through every dataset in work and convert every character variable to the X's.
There is a where condition using which I will be filtering out the desired data. So, it is ideally 20 tables and the specified columns from each table which have to be masked with 'XX..'
Thanks for this code as well :).
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.