DATA Step, Macro, Functions and more

Problem with macro variable as array dimension.

Reply
N/A
Posts: 0

Problem with macro variable as array dimension.

This code works:

%let years='9899';
%let yearcount=input('19' || substr(&years,3,2),best4.)-1950;
data mytable;
dim=&yearcount;
run;

I get the variable dim with value 49 in the data set mytable.

I continue with this (meaningless) code:

data mytable;
set mytable;
array x(&yearcount);
run;

Running it gives the following error message:
********************************************************************
NOTE: Line generated by the macro variable "yearcount".
1 input('19' || substr('9899',3,2),best4.)-1950

ERROR 79-322: Expecting a ).
ERROR 76-322: Syntax error, statement will be ignored.
*********************************************************************

The first left parenthesis in the text line: 1 input('19' || ......
is marked; underlined.

What's wrong?
Super Contributor
Posts: 474

Re: Problem with macro variable as array dimension.

Posted in reply to deleted_user
Try this instead for yearcount calculation:

%let years=9899;
%let yearcount=19%substr(&years,3,2)-1950; /* build the calculation */
%let yearcount=%eval(&yearcount); /* do the calculation */

This will resolve yearcount to:
49

Your code resolve yearcount to:
input('19' || substr('9899',3,2),best4.)-1950

Diference is, the way the calculation is performed, in your case at run time, in my sugestion at compile time.

And:

array x(input('19' || substr('9899',3,2),best4.)-1950);

is not a valid syntax, but:

array x(49)

is.

Greetings from Portugal.

Daniel Santos at www.cgd.pt
N/A
Posts: 0

Re: Problem with macro variable as array dimension.

Posted in reply to DanielSantos
Thank you.

But how do you explain that the first part worked, i.e. dim=&yearcount; assigned the value 49 to the variable dim?
Super Contributor
Posts: 474

Re: Problem with macro variable as array dimension.

Posted in reply to deleted_user
Hello AnneP.

Because:

dim=input('19' || substr('9899',3,2),best4.)-1950;

is a valid syntax.

but:

array x(input('19' || substr('9899',3,2),best4.)-1950);

is not.

Greetings from Portugal.

Daniel Santos at www.cgd.pt
N/A
Posts: 0

Re: Problem with macro variable as array dimension.

Posted in reply to DanielSantos
SAS made by sadists for masochists.
Super Contributor
Posts: 273

Re: Problem with macro variable as array dimension.

Posted in reply to deleted_user
Not at all ! To avoid to be furious,
open your eyes and read about the subscript rules on this page
http://support.sas.com/onlinedoc/913/getDoc/fr/lrdict.hlp/a000201956.htm

so if you simply submit this code, you have yet a rejection from SAS
as the allowed syntax is like that too
[pre]
28 data mytable;
29 set mytable;
30 array x(3+1);
--
22
ERROR 22-322: Syntax error, expecting one of the following: ',', :.

31 w=3;
ERROR: Invalid dimension specification for array x. The upper bound of an array dimension is smaller
than its corresponding lower bound.
32 array y (w) ;
-
22
ERROR 22-322: Syntax error, expecting one of the following: a name, _ALL_, _CHARACTER_, _CHAR_,
_NUMERIC_.

33 run;
[/pre]

If your 'input' text substitution was alone it could be accepted under certain condition

[pre]
47 data mytable;
48 set mytable;
49 input=3;
50 array z (input) _numeric_ ;
51 put z(*)=;
52 run;

dim=49 input=3
[/pre]

HTH
Andre
N/A
Posts: 0

Re: Problem with macro variable as array dimension.

It is important to remember that SAS Macro is basically a completely different program to SAS Base. The macro executes before the SAS code and writes SAS code for you.

When you can seperate the two and think of what results from your macro before the SAS code executes then things become a lot clearer.
SAS Super FREQ
Posts: 8,864

Re: Problem with macro variable as array dimension.

Posted in reply to deleted_user
Yes, I agree! That's why it is important to start with a WORKING SAS program. Because when a macro program or any macro trigger goes through the tokenization and resolution phase, what is happening is nothing more than TEXT SUBSTITUTION. So the macro processor is not, technically, "executing" any SAS code, but is more like a "pre-processing phase" for code, where code is being assembled and any references to &macvar or %macpgm are resolved. By the time your code gets to the SAS compiler, all &macvar and %macpgm references are gone.

The resolution could be as simple as one text string being substituted for an &macvar reference or it could be as complicated as calling %macpgm code which could resolve into one part of one statement, one complete statement, one whole program of one step or more than one step. This site describes what's going on:
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/getstart.htm

"The text substitution produced by the macro processor is completed before the program text is compiled and executed. The macro facility uses statements and functions that resemble the statements and functions that you use in the DATA step. An important difference, however, is that macro language elements can enable only text substitution and are not present during program or command execution. "

cynthia
Ask a Question
Discussion stats
  • 7 replies
  • 330 views
  • 0 likes
  • 4 in conversation