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;
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;
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.
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.
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".
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;
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!
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.