SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
polingjw
Quartz | Level 8
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]
Slash
Quartz | Level 8

Recently, I met the same problem, here is my solution:

 

 

%macro correct_sas_name(name);
%let flag=%sysfunc(notname(&name));
%do %until(&flag=0);
	%let char=%substr(&name,&flag,1);
	%let name=%sysfunc(TRANWRD(&name,&char,_));
	%let flag=%sysfunc(notname(&name));
%end;
h_&name
%mend;

%let test=SAS#123!34;
%let correct_name=%correct_sas_name(&test);

%put &test;
%put &correct_name;

 

The LOG:

LOG.jpg

 

andreas_lds
Jade | Level 19

Have a look at the function nliteral : Converts a character string that you specify to a SAS name literal. 

Slash
Quartz | Level 8

This is good way, thanks.

But, normally this way is not that common.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 18 replies
  • 7923 views
  • 9 likes
  • 10 in conversation