Hi SAS Users,
I have a problem running the following programs, the log screen gives me the following error message:
ERROR: Missing numeric suffix on a numbered variable list (p1-p).
ERROR: Undeclared array referenced: NAME.
ERROR: Variable NAME has not been declared as an array.
ERROR 22-322: Expecting a name.
My purpose is to aggregate purchase amount. So it would be something like:
want1=prchs1;
want2=prchs1+prchs2;
want3=prchs1+prchs2+prchs3;
.............
Could anyone please let me know what's wrong with my syntax below or any other suggested way to achieve my goal? Thanks in advance!
data want;
set have;
array want{12};
array p{12} prchs1-prchs12;
do i=1 to 12;
want[i]=0;
want[i] = sum(of p1 - p[i]);
end;
run;
What you want is not possible.
want[i] = sum(of p1 - p[i]);
You are mixing a compile time concept (the variable list) with a run time concetp (an array reference).
It actually is easy with this formula.
array want (12);
array p prchs1-prchs12;
want(1) = sum(0,p(1));
do i=2 to dim(p);
want(i)=sum(want(i-1),p(i));
end;
What you want is not possible.
want[i] = sum(of p1 - p[i]);
You are mixing a compile time concept (the variable list) with a run time concetp (an array reference).
It actually is easy with this formula.
array want (12);
array p prchs1-prchs12;
want(1) = sum(0,p(1));
do i=2 to dim(p);
want(i)=sum(want(i-1),p(i));
end;
The problem arises in referring to p[i] as part of a variable list. SAS requires the name to be hard-coded, rather than an indirect reference (when used as part of a variable list).
It's a relatively easy problem to overcome. For example, you could try:
want1 = prchs1;
do i=2 to 12;
want{i} = want{i-1} + p{i};
end;
You should define:
array want {12} want1-want12; Not just array want {12};
I would do it as follows:
data have; input prchs1-prchs12; cards; 1 2 3 4 5 6 7 8 9 0 1 2 2 3 4 5 6 7 8 9 0 1 2 3 ; data want; set have; array want{12}; array p{12} prchs1-prchs12; array s{12}; do i=1 to 12; want[i]=0; s(i)=p(i); want[i] = sum(of s(*)); end; run;
Art, CEO, AnalystFinder.com
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.