BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
atul_desh
Quartz | Level 8

%put &dsn;
File_x_20170116 File_Y_20170220 File_z_20170320 

 

%put %scan(&dsn,1,' ');
File_x_20170116

 

I want ouput from below function as '20170116' [digits from Fiile_x_20170116]

 

%put %sysfunc(compress(%scan(&dsn,1,' '), ,'kd'))
WARNING: In a call to the COMPRESS function or routine, the modifier "'" not valid.

 

is there any way to get it ?

 

Thanks

Atul Deshmukh

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You do not need to put quotes around string literals in macro code. Everything is a string to the macro processor.

So this code that you would use in a data step.

want = compress(scan(DSN,1,' '),,'kd');

Would look more like this in macro code.

%let want=%sysfunc(compress(%scan(&dsn,1,%str( )),,kd)) ;

What you ran was more like this in data step code.

want=compress(scan(DSN,1,"' '"),,"'kd'") ;

View solution in original post

3 REPLIES 3
art297
Opal | Level 21
%let dsn=File_x_20170116 File_Y_20170220 File_z_20170320;
 
%put %scan(&dsn,1,' ');
 
%put %sysfunc(compress(%scan(&dsn,1,' '),,kd));

Art, CEO, AnalystFinder.com

Astounding
PROC Star

Using COMPRESS with kd is dangerous.  It gives the wrong answer if digits appear elsewhere in the incoming file name.  You would be better off using:

 

%let final_digits = %scan(%scan(&dsn, 1, %str( )), -1, _);

Tom
Super User Tom
Super User

You do not need to put quotes around string literals in macro code. Everything is a string to the macro processor.

So this code that you would use in a data step.

want = compress(scan(DSN,1,' '),,'kd');

Would look more like this in macro code.

%let want=%sysfunc(compress(%scan(&dsn,1,%str( )),,kd)) ;

What you ran was more like this in data step code.

want=compress(scan(DSN,1,"' '"),,"'kd'") ;

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
  • 3 replies
  • 6569 views
  • 2 likes
  • 4 in conversation