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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 18 replies
  • 6483 views
  • 9 likes
  • 10 in conversation