BookmarkSubscribeRSS Feed
hpahuja84
Calcite | Level 5

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;

9 REPLIES 9
hpahuja84
Calcite | Level 5

Thanks Kurt,

 

So is there any alternate solution for this.

Kurt_Bremser
Super User

@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;
hpahuja84
Calcite | Level 5

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.

Reeza
Super User

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;


 

hpahuja84
Calcite | Level 5

Hi Reeza,

 

Thanks for this.

If i use %let a="P50001"; then it is also not working.

 

 

ChrisNZ
Tourmaline | Level 20

 

%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;


error_prone
Barite | Level 11
Why byte(80) and not 'P'?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 9 replies
  • 5959 views
  • 3 likes
  • 5 in conversation