DATA Step, Macro, Functions and more

Removing a character from a macro variable

Reply
New Contributor
Posts: 4

Removing a character from a macro variable

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;

Super User
Posts: 10,280

Re: Removing a character from a macro variable

Posted in reply to hpahuja84

You are in a data step, so %sysfunc is not needed, and most probably harmful.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
New Contributor
Posts: 4

Re: Removing a character from a macro variable

Posted in reply to KurtBremser

Thanks Kurt,

 

So is there any alternate solution for this.

Super User
Posts: 10,280

Re: Removing a character from a macro variable

Posted in reply to hpahuja84

@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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
New Contributor
Posts: 4

Re: Removing a character from a macro variable

Posted in reply to KurtBremser

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.

Super User
Posts: 23,776

Re: Removing a character from a macro variable

Posted in reply to hpahuja84

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;


 

New Contributor
Posts: 4

Re: Removing a character from a macro variable

Hi Reeza,

 

Thanks for this.

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

 

 

PROC Star
Posts: 2,370

Re: Removing a character from a macro variable

Posted in reply to hpahuja84

 

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


Regular Contributor
Posts: 213

Re: Removing a character from a macro variable

Posted in reply to hpahuja84
Why byte(80) and not 'P'?
Super User
Posts: 10,280

Re: Removing a character from a macro variable

Posted in reply to error_prone

@error_prone wrote:
Why byte(80) and not 'P'?

Exactly.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 9 replies
  • 139 views
  • 2 likes
  • 5 in conversation