macro data set name

Accepted Solution Solved
Reply
Regular Contributor
Posts: 161
Accepted Solution

macro data set name

[ Edited ]

I want to create sequences of sas data set macros

for example &site  is a subset of site 

then I want to create &site1 which is the subset of &site 

  then &site2 which is a subset of &site1 

 

Do you know what is the best way to do it, bc when I tried to do it this way &&site1

it shows error in the log 

 

%let site=site; 


PROC IMPORT OUT=&site
DATAFILE= "&Path1"
DBMS=XLSX REPLACE;
run;


data &&site1;
set &site;
run;
92 &site1
_
22
200
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, /, ;, _DATA_, _LAST_, _NULL_.
 
ERROR 200-322: The symbol is not recognized and will be ignored.

 


Accepted Solutions
Solution
‎03-05-2018 12:03 PM
Super User
Super User
Posts: 8,127

Re: macro data set name

How did you expect the SAS macro processor to interpret these lines?  

data &&site1; 
  set &site; 
run;

That is what lines of actual SAS code did you expect to result after the macro variable references were replaced?

 

What will resolve to in pseudo code is:

data <value of macro variable SITE1>; 
  set <value of macro variable SITE>; 
run;

If instead you wanted to append the constant text 1 to the end of the value of SITE you need to let the macro processor know where you macro variable name ends and your constant text begins.  You use a period for that.

data &site.1; 
  set &site; 
run;

Note that means if the constant text starts with a period you will need to type two of them. One for the macro processor and one for SAS.

data _null_;
  set &site; 
  file "outfile_&site..txt" dsd ;
  put (_all_) (+0);
run;

View solution in original post


All Replies
Super User
Posts: 10,280

Re: macro data set name

Please put yourself in the position of someone who does not know your intentions or your code, and then read your post. Would you be able to make any sense of it?

 

Post code and log, example data (data step!), and either point to the ERROR/WARNING or, in the case of (on the surface) working code, where the result differs from your expectations.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,599

Re: macro data set name

This: "

data &&site1; 
set &site;
run; 

Is never going to end well.  Import your data, then apply your groupings in one dataset.  Never split data into multiple datasets unless there is a very good reason (and there rarely is).  Also don't put data - in this case site - in dataset names or variables, these are for programming.   

Super User
Posts: 13,583

Re: macro data set name

Instead of

data &&site1; 
set &site; 
run;

try

 

data &site.1; 
set &site; 
run;

Though I agree with @RW9 about splitting data.

 

 

Reason: when SAS encounters something like &&Site1 it "holds" the first & and tries to resolve "&site1" and then resolve the resulting &site1. Suppose you had a macro variable Site1 with the value of ABC. Then &&site1 by steps becomes&(&site1) becomes &ABC. So SAS would expect a macro variable named ABC and use the value of that variable.

The &site.1 , pay attention to the period says "resolve the macro variable site and append 1 to the value".

Solution
‎03-05-2018 12:03 PM
Super User
Super User
Posts: 8,127

Re: macro data set name

How did you expect the SAS macro processor to interpret these lines?  

data &&site1; 
  set &site; 
run;

That is what lines of actual SAS code did you expect to result after the macro variable references were replaced?

 

What will resolve to in pseudo code is:

data <value of macro variable SITE1>; 
  set <value of macro variable SITE>; 
run;

If instead you wanted to append the constant text 1 to the end of the value of SITE you need to let the macro processor know where you macro variable name ends and your constant text begins.  You use a period for that.

data &site.1; 
  set &site; 
run;

Note that means if the constant text starts with a period you will need to type two of them. One for the macro processor and one for SAS.

data _null_;
  set &site; 
  file "outfile_&site..txt" dsd ;
  put (_all_) (+0);
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 137 views
  • 3 likes
  • 5 in conversation