DATA Step, Macro, Functions and more

Macro (remove space within a variable)

Posts: 62

Macro (remove space within a variable)

I want to create a data set with following macro variable.


%let x =credit data;


data &x._&sysdate9.;

set t;


Getting error, can not we remove the space within  a macro variable by using some function?

Valued Guide
Posts: 570

Re: Macro (remove space within a variable)

You can remove the space like this (note that you need to remove the dot before _&sysdate9


%let x =credit data;
data %sysfunc(compress(&x))_&sysdate9.;
set sashelp.class;
Trusted Advisor
Posts: 1,831

Re: Macro (remove space within a variable)

Posted in reply to ChrisBrooks

DATA statement defines input dataset name in a format of <libref>.<DatasetName>.

LIBREF is defined by LIBNAME <libref> "path to data" and is limited to SAS names rules:

taken from



is a shortcut name or a "nickname" for the aggregate storage location where your SAS files are stored. It is any SAS name when you are assigning a new libref. When you are disassociating a libref from a SAS library or when you are listing attributes, specify a libref that was previously assigned.

Range: 1 to 8 characters
Tip: The association between a libref and a SAS library lasts only for the duration of the SAS session or until you change it or discontinue it with another LIBNAME statement.



therefore, as compressed &x is 'CreditData' - 10 characters long, it will end with error - illegal libref name.


Respected Advisor
Posts: 4,679

Re: Macro (remove space within a variable)

[ Edited ]


I believe you've missed that the dot after &x will be consumed by the macro processor and though &x becomes part of the table name which can be up to 32 characters.

@ChrisBrooks code looks like a suitable solution to me.

Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation