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; run;
however when i try using the variable names like the below, it doesnt work:
data Var1, set Var2; run;
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;
run;
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;
run;
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.
Hi,
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 ; 2 3 data FullList; 4 set Items&20220101; - 22 200 ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, -, :, ;, CUROBS, END, INDSNAME, KEY, KEYRESET, KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_. 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. 6 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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.