DATA Step, Macro, Functions and more

replace a foreign character in a string

Reply
N/A
Posts: 0

replace a foreign character in a string

Hi,

I am trying to replace a foreign character in a string, unfortunatelly most of useful string functions such as "tranwrd" does not work because SAS complains that it is not a valide SAS name. Right now, I created a macro. In the macro I get the length of the string, then use "substr" to get a single letter at a time, then do a cat afterwards.

Does anyone have a better idea please?

Thanks
Qun
Super User
Posts: 10,020

Re: replace a foreign character in a string

Posted in reply to deleted_user
options validvarname=any;
[pre]


Then you can use invalid sas varname.



Ksharp
[/pre]
Super Contributor
Super Contributor
Posts: 3,174

Re: replace a foreign character in a string

Share the actual SAS log with the specific error message revealed -- include all SAS program code revealed. You can do this with a COPY/PASTE into your next forum post-reply.

Also add this statement to generate additional diagnostic message with all variable values displayed to the log:

PUTLOG / _ALL_;

Scott Barry
SBBWorks, Inc. Message was edited by: sbb
N/A
Posts: 0

Re: replace a foreign character in a string

Many thanks, Ksharp. I should've looked at the options in the first place.

Yan
Respected Advisor
Posts: 3,799

Re: replace a foreign character in a string

Posted in reply to deleted_user
It is unclear to me what you mean by foreign character. And if the characters are in variable values of variable names.

For values of variables check out COMPRESS.
Super User
Posts: 10,020

Re: replace a foreign character in a string

Posted in reply to data_null__
Hi. _null_
For the foreign character, SAS has some specical function to process this situation.
For example : Kcompress()


Ksharp
N/A
Posts: 0

Re: replace a foreign character in a string

I am confused as why the following code didn't work as I expected


options validvarname=any;

%macro test();
%global new;
%let old = ppø;
%let new = %sysfunc(tranwrd(&old, 'ø', 'aa'));
%put &new;
%mend;

%test();

%put &new;

The result is

MLOGIC(TEST): Beginning execution.
MLOGIC(TEST): %LET (variable name is NEW)
SYMBOLGEN: Macro variable OLD resolves to ppø
MLOGIC(TEST): %PUT &new
SYMBOLGEN: Macro variable NEW resolves to ppø
ppø
MLOGIC(TEST): Ending execution.
3832
3833 %put &new;
SYMBOLGEN: Macro variable NEW resolves to ppø
ppø


I would thought "new" should be "ppaa".


Many thanks,
Qun
N/A
Posts: 0

Re: replace a foreign character in a string

Posted in reply to deleted_user
Sorry, cut past the wrong code

options validvarname=any;

%let old=ppø;
%let new=&old;

%macro test();
%let new = %sysfunc(tranwrd(&old, 'ø', 'aa'));
%put &new;
%mend;

%test();

%put &new;
N/A
Posts: 0

Re: replace a foreign character in a string

Posted in reply to deleted_user
It's one of those issues with quote. Once I removed the quote around ø, and aa, it works.

I can never be sure when I suppose to ", or ' in data step or in macro.

Qun
SAS Super FREQ
Posts: 8,864

Re: replace a foreign character in a string

Posted in reply to deleted_user
This is a good introduction to the SAS Macro facility:
http://www2.sas.com/proceedings/sugi28/056-28.pdf

cynthia
Super User
Posts: 10,020

Re: replace a foreign character in a string

Posted in reply to deleted_user
Hi.
_null_ is right .Macro facility treat anything as text ,even if it is the argument of function.


Ksharp
Respected Advisor
Posts: 3,799

Re: replace a foreign character in a string

Posted in reply to deleted_user
Don't use "quotes" 'quotes' in macro language.

1632 %let old=ppø;
1633 %let new=&old;
1634
1635 %let new = %sysfunc(tranwrd(&old, ø, aa));
1636 %put NOTE &new;
NOTE ppaa
Ask a Question
Discussion stats
  • 11 replies
  • 264 views
  • 0 likes
  • 5 in conversation