DATA Step, Macro, Functions and more

Do Loop Question_missing numeric suffix on a numbered variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Do Loop Question_missing numeric suffix on a numbered variable

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;

 


Accepted Solutions
Solution
‎05-02-2017 01:56 PM
Super User
Super User
Posts: 6,497

Re: Do Loop Question_missing numeric suffix on a numbered variable

[ Edited ]

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;

 

View solution in original post


All Replies
Solution
‎05-02-2017 01:56 PM
Super User
Super User
Posts: 6,497

Re: Do Loop Question_missing numeric suffix on a numbered variable

[ Edited ]

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;

 

Super User
Posts: 5,071

Re: Do Loop Question_missing numeric suffix on a numbered variable

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;

Trusted Advisor
Posts: 1,353

Re: Do Loop Question_missing numeric suffix on a numbered variable

You should define:

 

array want {12} want1-want12;  Not just array want {12};

PROC Star
Posts: 7,356

Re: Do Loop Question_missing numeric suffix on a numbered variable

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

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 144 views
  • 3 likes
  • 5 in conversation