BookmarkSubscribeRSS Feed
Obsidian | Level 7

Hi there


I have a filename saved in a variable

Var1 = Items&20220101

and I dataset I want to created with the name FullList

Var2 = FullList


by doing the below it works:

data FullList;

 set Items&20220101;


however when i try using the variable names like the below, it doesnt work:

data Var1,
set Var2;

I have been getting a whole lot of errors when trying various options, e.g. using the quote functions, using str functions, using compress etc. 



For better or worse, this is my impression.  It sounds like you are experienced in other programming languages, but not in SAS.  It also sounds like you have performed many tests and the multitude of tests and results are starting to blend together.  Where I can guide you ...


There is no such thing as a data set name that contains an ampersand.  This would never work:

 set Items&20220101;

But let's assume that this program would work because the data set Items20220101 actually exists:

data FullList;
  set Items20220101;

To treat the data set names as variables, you would need to use macro language.  First the warning.  You should not do this.  To use macro language, you really need more SAS experience.  Macro language does not construct a data set, it constructs a program.  When the program executes, that is what constructs the data set.  It is not a beginner's topic.  That said, here's the idea of what could work:

%let var1 = FullList;
%let var2 = Items20220101;
data &var1;
  set &var2;

Macro language replaces references to &var1 and &var2 with the strings that they contain.  So this does not violate the rule that data set names cannot contain ampersands.  Instead, it indicates that macro language needs to replace the references to &var1 and &var2 to identify to SAS what the names of the data sets should be.


Or perhaps I have totally mis-identified the problem and you have something else in mind.


Super User



Can you please show the log you get from both the working code, and non-working code?  Do you intend for VAR1 and VAR2 to be macro variables?


I could not get the code you say is working to work.  It's unusual to create a dataset with an & in the name.  Apparently it can be done if you turn on ValidMemName=extend.  But even with that, you need to use a name literal to reference the data set.


1    options validmemname=extend ;
3    data FullList;
4     set Items&20220101;
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, -, :, ;,

ERROR 200-322: The symbol is not recognized and will be ignored.

5    run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.FULLLIST may be incomplete.  When this step was stopped there were 0
         observations and 5 variables.
WARNING: Data set WORK.FULLLIST was not replaced because this step was stopped.

7    data FullList;
8     set "Items&20220101"n;
9    run;

NOTE: There were 1 observations read from the data set WORK.'ITEMS&20220101'n.
NOTE: The data set WORK.FULLLIST has 1 observations and 0 variables.



BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page:


Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.


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
  • 2 replies
  • 3 in conversation