DATA Step, Macro, Functions and more

how to use compress keep digit on macro variable

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

how to use compress keep digit on macro variable

%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


Accepted Solutions
Solution
‎07-18-2017 11:29 AM
Super User
Super User
Posts: 7,046

Re: how to use compress keep digit on macro variable

[ Edited ]
Posted in reply to atul_desh

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


All Replies
PROC Star
Posts: 7,473

Re: how to use compress keep digit on macro variable

Posted in reply to atul_desh
%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

Super User
Posts: 5,504

Re: how to use compress keep digit on macro variable

Posted in reply to atul_desh

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, _);

Solution
‎07-18-2017 11:29 AM
Super User
Super User
Posts: 7,046

Re: how to use compress keep digit on macro variable

[ Edited ]
Posted in reply to atul_desh

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'") ;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 155 views
  • 2 likes
  • 4 in conversation