BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5
%let a= abcd efgh ijkl ;

%put %cmpres(&a) ;

Above a macro variable not compressing.
The %cmpres how works
I need value like abcdefghijkl
3 REPLIES 3
Shmuel
Garnet | Level 18

Either run code preceding with options mprint symbolgen; and post the log

Or change code to %compress(&a);

jvdl
Obsidian | Level 7

The %CMPRES function removes multiple spaces, as well as leading and trailing spaces. However, it will preserve single spaces in between.

 

Try the following:

%let a= abcd efgh ijkl ;
%put %sysfunc(compress(&a));

 

Tom
Super User Tom
Super User

Note that system macro is ancient.  You can use %SYSFUNC() now and avoid those macro loops.  To get similar results to %CMPRES() use %SYSFUNC(COMPBL()).  To remove all spaces use %SYSFUNC(COMPRESS()).

152   %let a= abcd    efgh ijkl ;
153
154   %put a        |&a| ;
a        |abcd    efgh ijkl|
155   %put cmpress  |%cmpres(&a)| ;
cmpress  |abcd efgh ijkl|
156   %put compbl   |%sysfunc(compbl(&a))| ;
compbl   |abcd efgh ijkl|
157   %put compress |%sysfunc(compress(&a))| ;
compress |abcdefghijkl|

 

If you want to see how CMPRES works you can read the code.

%macro cmpres(text);
%*********************************************************************;
%*                                                                   *;
%*  MACRO: CMPRES                                                    *;
%*                                                                   *;
%*  USAGE: 1) %cmpres(argument)                                      *;
%*                                                                   *;
%*  DESCRIPTION:                                                     *;
%*    This macro returns the argument passed to it in an unquoted    *;
%*    form with multiple blanks compressed to single blanks and also *;
%*    with leading and trailing blanks removed.                      *;
%*                                                                   *;
%*    Eg. %let macvar=%cmpres(&argtext)                              *;
%*                                                                   *;
%*  NOTES:                                                           *;
%*    The %LEFT and %TRIM macros in the autocall library are used    *;
%*    in this macro.                                                 *;
%*                                                                   *;
%*********************************************************************;
%local i;
%let i=%index(&text,%str(  ));
%do %while(&i ne 0);
  %let text=%qsubstr(&text,1,&i)%qleft(%qsubstr(&text,&i+1));
  %let i=%index(&text,%str(  ));
%end;
%left(%qtrim(&text))
%mend;

Of course you will also need the code for the %LEFT() and %QTRIM() macros.

%macro left(text);                                                      
%*********************************************************************; 
%*                                                                   *; 
%*  MACRO: LEFT                                                      *; 
%*                                                                   *; 
%*  USAGE: 1) %left(argument)                                        *; 
%*                                                                   *; 
%*  DESCRIPTION:                                                     *; 
%*    This macro returns the argument passed to it without any       *; 
%*    leading blanks in an unquoted form. The syntax for its use     *; 
%*    is similar to that of native macro functions.                  *; 
%*                                                                   *; 
%*    Eg. %let macvar=%left(&argtext)                                *; 
%*                                                                   *; 
%*  NOTES:                                                           *; 
%*    The %VERIFY macro is used to determine the first non-blank     *; 
%*    character position.                                            *; 
%*                                                                   *; 
%*********************************************************************; 
%local i;                                                               
%if %length(&text)=0 %then %let text=%str( );                           
%let i=%verify(&text,%str( ));                                          
%if &i %then %substr(&text,&i);                                         
%mend;    
%macro qtrim(value);
%*********************************************************************;
%*                                                                   *;
%*  MACRO: QTRIM                                                     *;
%*                                                                   *;
%*  USAGE: 1) %qtrim(argument)                                       *;
%*                                                                   *;
%*  DESCRIPTION:                                                     *;
%*    This macro returns the argument passed to it without any       *;
%*    trailing blanks in a quoted form. The syntax for its use       *;
%*    is similar to that of native macro functions.                  *;
%*                                                                   *;
%*    Eg. %let macvar=%qtrim(&argtext)                               *;
%*                                                                   *;
%*  NOTES:                                                           *;
%*    None.                                                          *;
%*                                                                   *;
%*********************************************************************;
  %local i;
  %do i=%length(&value) %to 1 %by -1;
    %if %qsubstr(&value,&i,1) ne %str( ) %then %goto trimmed;
  %end;
  %trimmed: %if &i>0 %then %qsubstr(&value,1,&i);
%mend;

Which to understand you will have to read the VERIFY macro.

%macro verify(text,target);
%*********************************************************************;
%*                                                                   *;
%*  MACRO: VERIFY                                                    *;
%*                                                                   *;
%*  USAGE: 1) %verify(argument,target)                               *;
%*                                                                   *;
%*  DESCRIPTION:                                                     *;
%*    This macro returns the position of the first character in the  *;
%*    argument that is not in the target value. If every character   *;
%*    that is in the argument is also in the target value then this  *;
%*    fuction returns a value of 0. The syntax for its use is        *;
%*    is similar to that of native macro functions.                  *;
%*                                                                   *;
%*    Eg. %let i=%verify(&argtext,&targtext)                         *;
%*                                                                   *;
%*  SUPPORT: sassmo                                                  *;
%*  NOTES:                                                           *;
%*    Both values to this function must have non-zero lengths or an  *;
%*    error message will be produced.                                *;
%*                                                                   *;
%*********************************************************************;
%local i;
%if %length(&text)=0 OR %length(&target)=0 %then %do;
  %put ERROR: ARGUMENT TO VERIFY FUNCTION MISSING.;

  %goto errout;
%end;
%do i=1 %to %length(&text);
  %if NOT %index(&target,%qsubstr(&text,&i,1)) %then %goto verfnd;
%end;
%verfnd: %if &i>%length(&text) %then 0;
           %else &i;
%errout:
%mend;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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