BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mona4u
Lapis Lazuli | Level 10

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

4 REPLIES 4
Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.   

ballardw
Super User

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

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 6180 views
  • 3 likes
  • 5 in conversation