SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sean_OConnor
Obsidian | Level 7

Folks,

 

I'm following the steps in the above example to attempt to create a macro which will read over various values. However, I'm running into trouble.

 

https://blogs.sas.com/content/sastraining/2015/01/30/sas-authors-tip-getting-the-macro-language-to-p...

 

So I want my code to read in the following values (1e, 1f etc.)

 

And then spit out 7 new datasets where they would be called 

 

test1e , test1f, test1g etc. 

 

%macro px;

%let value = 1e 1f 1g 1h 1b 1a 1c;


%local i next_value;
%let i=1;
%do %while (%scan(&value, &i) ne );
   %let next_value = %scan(&value, &i);
   %let i = %eval(&i + 1);
%end;

   data test&value;
set table_&value;run;
%mend;

%px;

However, I'm running into the following issues.

 

NOTE: Line generated by the macro variable "VALUE".
44          test1e 1f 1g 1h 1b 1a 1c
                   _     _     _
                   22    22    22
                   200   200   200
                      _     _     _
                      22    22    22
                      200   200   200
SYMBOLGEN:  Macro variable VALUE resolves to 1e 1f 1g 1h 1b 1a 1c
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.

NOTE: Line generated by the macro variable "VALUE".
44          table_1e 1f 1g 1h 1b 1a 1c
                     _     _     _
                     22    22    22
                     200   200   200
                        _     _     _
                        22    22    22
                        200   200   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.

It doesn't seem to be looping over things correctly.

 

Any input would be great

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

&VALUE is constant.  &NEXT_VALUE is what changes each time through the loop.

 

Whatever it is you want to generate as a SAS program, it has to be inside the %DO loop, not outside.  And it should refer to &NEXT_VALUE instead of &VALUE.

View solution in original post

4 REPLIES 4
ballardw
Super User

Why did you bother to create next_value if you weren't going to use it?

I think that is the macro variable you meant to use instead of value (which has 5 spaces in the middle) in the data step.

Astounding
PROC Star

&VALUE is constant.  &NEXT_VALUE is what changes each time through the loop.

 

Whatever it is you want to generate as a SAS program, it has to be inside the %DO loop, not outside.  And it should refer to &NEXT_VALUE instead of &VALUE.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Look at what you macro code resolves to - remember macro does nothing but create base SAS code:

data test1e 1f 1g 1h 1b 1a 1c;  set table_1e 1f 1g 1h 1b 1a 1c;run;

 

Does that look right to you?  Maybe you meant to use next_value.

 

Oh, and the usual word of warning, macro can be helpful in some select occasions, however it is not a replacement for base SAS.  If you find yourself doing loops and lists of values in macro, then 90% of the time you are doing something wrong.

bstarr
Quartz | Level 8

Hi,

 

If I understand correctly what you're trying to do, your ordering of SAS statements is a bit off, and you use the original long string &VALUE as the dataset name. The %do loop ends without actually doing anything besides incrementing i and creating next_value macro variable. I assume for purposes of the example, the input dataset is exactly the same as the output dataset.

 

Try:

 

%macro px;

%let value = 1e 1f 1g 1h 1b 1a 1c;


%local i next_value;
%let i=1;
%do %while (%scan(&value, &i) ne );
   %let next_value = %scan(&value, &i);

   data test&next_value;
set table_&next_value;run;
%let i = %eval(&i + 1);

%end;

%mend;

%px;

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 50953 views
  • 0 likes
  • 5 in conversation