DATA Step, Macro, Functions and more

SAS Function to convert string to Legal SAS Name?

Reply
Super Contributor
Posts: 281

SAS Function to convert string to Legal SAS Name?

Suppose I have an arbitrary text string, such as

ACD(#)DEF

This is not a legal SAS variable name. Is there a function that will convert this to a legal SAS name?

I know I can do this with translate function, but then I would need to know every possible illegal character that might appear.
SAS Employee
Posts: 27

Re: SAS Function to convert string to Legal SAS Name?

I haven't found a function to convert a string to a SAS variable name, but you might have a look at these functions; they might help you get where you need to go.

notname function: http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a002197357.htm

nvalid function: http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a002271952.htm
Valued Guide
Posts: 2,175

Re: SAS Function to convert string to Legal SAS Name?

it sounds like a good application for a prxchange()

any prx specialists on-line?
Super Contributor
Posts: 394

Re: SAS Function to convert string to Legal SAS Name?

> I would need to know every possible illegal
> character that might appear.

Not really. You only need to know what characters are legal. The first character must be alphabetic or an underscore. Subsequent characters can be alphabetic, numeric, or underscores. (See Rules for SAS Variable Names.) Identify legal characters and substitute a legal character (such as '_') for the others.

Or perhaps converting these strings to name literals will do the trick for you? Use the VALIDVARNAME=ANY option and change ABC(#)DEF to 'ABC(#)DEF'n.
Super Contributor
Posts: 281

Re: SAS Function to convert string to Legal SAS Name?

> > I would need to know every possible illegal
> > character that might appear.
>
> Not really. You only need to know what characters are
> legal. The first character must be alphabetic or an
> underscore. Subsequent characters can be alphabetic,
> numeric, or underscores. (See
> Rules
> for SAS Variable Names
.) Identify legal
> characters and substitute a legal character (such as
> '_') for the others.

Your solution still requires me to go through the string character by character, and translate as needed. Which is what I was hoping to avoid. Actually, my idea of using TRANSLATE is a whole lot easier for me to program than looping through each character. I was hoping there was a SAS function that would do the job for me.
Respected Advisor
Posts: 3,777

Re: SAS Function to convert string to Legal SAS Name?

Post=500

I've wondered about this myself. SAS clearly knows how to do this, with the various IMPORT methods through SAS/ACCESS.

> Your solution still requires me to go through the
> string character by character, and translate as
> needed.

You don't actually need to loop every character. NOTNAME returns character number and when it returns 0 there are no more NOTNAME characters.
NVALID will tell you if you need to try at all.

This might be a good opportunity to learn PROC FCMP.

[pre]
1043 data _null_;
1044 length name $32;
1045 do name = 'ACD(#)DEF','SAS','Hello','1','1C',' ACD(#)DEF ';
1046 if not nvalid(name) then do;
1047 notname = notname(trimN(name));
1048 do while(notname);
1049 substr(name,notname,1)='_';
1050 notname = notname(trimN(name));
1051 end;
1052 if not anyfirst(first(name)) then name = cats('_',name);
1053 end;
1054 put 'NOTE: ' name=;
1055 end;
1056 run;

NOTE: name=ACD___DEF
NOTE: name=SAS
NOTE: name=Hello
NOTE: name=_1
NOTE: name=_1C
NOTE: name=_ACD___DEF
[/pre]
Super Contributor
Posts: 394

Re: SAS Function to convert string to Legal SAS Name?

Does the solution need to ensure that distinct strings produce distinct names? For example, should "ABC#DEF" and "ABC!DEF" be converted to two different names?
Respected Advisor
Posts: 3,777

Re: SAS Function to convert string to Legal SAS Name?

I would think YES. Which is a bit complicated than just changing NOTNAME characters.
Valued Guide
Posts: 2,175

Re: SAS Function to convert string to Legal SAS Name?

could push into a hash table
Super Contributor
Posts: 281

Re: SAS Function to convert string to Legal SAS Name?

> Does the solution need to ensure that distinct
> strings produce distinct names? For example, should
> "ABC#DEF" and "ABC!DEF" be converted to two different
> names?

For my purposes, the answer is NO, because the incoming strings aren't truly arbitrary. However, I could see situations where the answer would be YES.
Regular Contributor
Posts: 171

Re: SAS Function to convert string to Legal SAS Name?

Here is another quick solution if it is acceptable to simply remove all the invalid characters from the string:

[pre]
95 data _null_;
96 string = ' &@ 03 This Is &An. Invalid "SAS" Variable ± Name;';
97 ValidName=compress(string, '_', 'adik');
98 if anydigit(ValidName) = 1 then validname= substr(ValidName,anyalpha(ValidName));
99 put ValidName=;
100 run;

ValidName=ThisIsAnInvalidSASVariableName
[/pre]
Regular Contributor
Posts: 171

Re: SAS Function to convert string to Legal SAS Name?

Instead of using translate to check for every possible illegal character that might appear, as originally proposed, how about using translate to change only the illegal characters that actually do appear? For example:

[pre]
37 data _null_;
38 length valid_name $ 32;
39 string = 'ABC(#)DEF';
40 valid_name = translate(string, repeat('_', 50), compress(string,'_', 'adi'));
41 if anydigit(valid_name)=1 then valid_name = '_'!!valid_name;
42 put valid_name=;
43 run;

valid_name=ABC___DEF
[/pre]
Ask a Question
Discussion stats
  • 11 replies
  • 1059 views
  • 3 likes
  • 6 in conversation