BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
7 REPLIES 7
DanielSantos
Barite | Level 11
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
deleted_user
Not applicable
Thank you.

But how do you explain that the first part worked, i.e. dim=&yearcount; assigned the value 49 to the variable dim?
DanielSantos
Barite | Level 11
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
deleted_user
Not applicable
SAS made by sadists for masochists.
Andre
Obsidian | Level 7
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
deleted_user
Not applicable
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.
Cynthia_sas
SAS Super FREQ
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

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
  • 7 replies
  • 1389 views
  • 0 likes
  • 4 in conversation