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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 5100 views
  • 9 likes
  • 10 in conversation