Hello,
I want to remove character from macro variable. For example in below code, i want to remove character P so i am using byte(80). but
its not working and instead of P it is removing 0 . I dont want to use "P" as second argument .Only byte or hexadecimal is allowed.
%let a="p50001";
data z;
c=%sysfunc(compress(&a,byte(80)));
run;
You are in a data step, so %sysfunc is not needed, and most probably harmful.
Thanks Kurt,
So is there any alternate solution for this.
@hpahuja84 wrote:
Thanks Kurt,
So is there any alternate solution for this.
First of all, as soon as you use the macro function %sysfunc, the whole argument inside that function turns into macro code and needs to be written as such:
%let a="P50001";
data z;
c=%sysfunc(compress(&a,%sysfunc(byte(80))));
run;
Without the inner sysfunc, the compress function will receive the string "byte(80)" as its second argument, and remove all the zeros.
And I do not see why one would use the byte() function to denote a single character, when you only have to write that character as such:
%let a=P50001;
data z;
c=compress("&a","P");
run;
If, of course, you want to write unmaintainable obfuscated code, you should go some steps further:
%let a="P50001";
data z;
c=%sysfunc(compress(&a,%sysfunc(byte(%eval(%sysfunc(intnx(month,'01jan1960'd,2,s))+20)))));
run;
Thankyou very much.
It is working now. I used byte function because in some other examples, i have to use same logic to remove non printable characters for which byte function is only needed.
Byte 80 is a capital P, and your variable is lower case. Strings are case sensitive.
@hpahuja84 wrote:
Hello,
I want to remove character from macro variable. For example in below code, i want to remove character P so i am using byte(80). but
its not working and instead of P it is removing 0 . I dont want to use "P" as second argument .Only byte or hexadecimal is allowed.
%let a="p50001";
data z;
c=%sysfunc(compress(&a,byte(80)));
run;
Hi Reeza,
Thanks for this.
If i use %let a="P50001"; then it is also not working.
%let a="P50001";
data z;
c=compress(&a,byte(80));
run;
or even better:
%let a=P50001; data z; c=compress("&a",byte(80)); run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.