BookmarkSubscribeRSS Feed
Citrine10
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;

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. 

 

2 REPLIES 2
Astounding
PROC Star

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.

 

Quentin
Super User

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.

 

 

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 440 views
  • 0 likes
  • 3 in conversation