BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

Please find a question regarding array.

Way3 (creating array) is not working.(get error).

ERROR: Undeclared array referenced: var1.

 

Please don't ask me to change the structure of data or stop define macro parameters.

There are macro parameters because it is a dynamic data that changes everyday.

I just want to know how to write Array correctly in this example and what is wrong with it

thank you

 

Data ttt2;
set ttt;
/*Way1*/
/*PCT1=(X1906-X1905)/X1905*100;*/
/*PCT2=(X1907-X1906)/X1906*100;*/
/*PCT3=(X1908-X1907)/X1907*100;*/
/*PCT4=(X1909-X1908)/X1908*100;*/

/*Way2*/
/*PCT1=(X&m1.-X&m0.)/X&m0.*100;*/
/*PCT2=(X&m2.-X&m1.)/X&m1.*100;*/
/*PCT3=(X&m3.-X&m2.)/X&m2.*100;*/
/*PCT4=(X&m4.-X&m3.)/X&m3.*100;*/

/*Way3-There is an error and I want to learn why*/
Array Var PCT1-PCT4;
Do j=1 To 4;
var1(j)=(X&mj.-X&mj-1)/X&mj. ;
END;
Run;
 

 

10 REPLIES 10
gamotte
Rhodochrosite | Level 12

Hint : read the error message then read the last lines of your code and try to figure out what is wrong.

Ronein
Meteorite | Level 14

Sorry,

I didn't understand how to solve it.

I think that error  is related to j-1

 

Ronein
Meteorite | Level 14

I found one error (by mistake i wrote VAR1 instead of VAR).

Now, i still have error

43 var(j)=(X&mj.-X&mj-1)/X&mj. ;
___ ___
22 22
201 201

Data ttt2;
set ttt;
/*Way1*/
/*PCT1=(X1906-X1905)/X1905*100;*/
/*PCT2=(X1907-X1906)/X1906*100;*/
/*PCT3=(X1908-X1907)/X1907*100;*/
/*PCT4=(X1909-X1908)/X1908*100;*/

/*Way2*/
/*PCT1=(X&m1.-X&m0.)/X&m0.*100;*/
/*PCT2=(X&m2.-X&m1.)/X&m1.*100;*/
/*PCT3=(X&m3.-X&m2.)/X&m2.*100;*/
/*PCT4=(X&m4.-X&m3.)/X&m3.*100;*/

/*Way3-There is an error and I want to learn why*/
Array Var PCT1-PCT4;
Do j=1 To dim(Var);
var(j)=(X&mj.-X&mj-1)/X&mj. ;
END;
Run;
Kurt_Bremser
Super User

What is contained in the macro variable &mj? And do you have the variable names created by the use of that macro variable in your dataset?

 

PS Use the {i} button for posting logs and log snippets.

Patrick
Opal | Level 21

1. Don't use round brackets but square or curly ones.

var[j]=.....

2. Is your formula really doing what you want it to do?

var[j]=(X&mj.-X&mj-1)/X&mj. ;
...is the same as...
var[j]= -1/X&mj. ;

 

 

Ronein
Meteorite | Level 14

Hello and thank you .

var[j]=(X&mj.-X&mj-1)/X&mj. ;

 

j-1 is the index of M macro varaible

For example:

(X&m1. - X&m0.) /X&m0.

 

 

 

FreelanceReinh
Jade | Level 19

@Ronein wrote:

j-1 is the index of M macro varaible


Sure, but the values of DATA step variable j, which are created during execution time, won't be "inserted" into macro variable reference &mj, which needs to be resolved already at compile time. While you could use an awkward construct such as

input(vvaluex(cat('X',symget(cat('m',j)))),32.)

I'd rather recommend something like this (given your data structure and macro variables m0=1905, ..., m4=1909):

/* Create test data for demonstration */

data ttt;
array a x1905-x1909 (100 105 120 150 160);
run;

/* Compute percentage changes */

%let d=4;

data ttt2(drop=j);
set ttt;
array pct[&d];
array v[0:&d] x&m0--x&&m&d;
do j=1 to &d;
  pct[j]=(v[j]/v[j-1]-1)*100;
end;
run;

Note the requirements of the name range variable list in the definition of array v (which I used to avoid issues when the YYMM list crosses a year boundary like 1912, 2001, ...).

Kurt_Bremser
Super User

Transpose your dataset to a long structure, and the whole thing is simple by using the lag() function.

data have;
input period $ x;
datalines;
1905 100
1906 200
1907 250
;

data want;
set have;
pct = (x - lag(x)) / x;
run;
Reeza
Super User

This is much easier if you show the log. The log will indicate where the error is by where it appears and usually even has the line and column number so you can better identify the issue. Working with just an error message isn't enough. 

 


@Ronein wrote:

I found one error (by mistake i wrote VAR1 instead of VAR).

Now, i still have error

43 var(j)=(X&mj.-X&mj-1)/X&mj. ;
___ ___
22 22
201 201

Data ttt2;
set ttt;
/*Way1*/
/*PCT1=(X1906-X1905)/X1905*100;*/
/*PCT2=(X1907-X1906)/X1906*100;*/
/*PCT3=(X1908-X1907)/X1907*100;*/
/*PCT4=(X1909-X1908)/X1908*100;*/

/*Way2*/
/*PCT1=(X&m1.-X&m0.)/X&m0.*100;*/
/*PCT2=(X&m2.-X&m1.)/X&m1.*100;*/
/*PCT3=(X&m3.-X&m2.)/X&m2.*100;*/
/*PCT4=(X&m4.-X&m3.)/X&m3.*100;*/

/*Way3-There is an error and I want to learn why*/
Array Var PCT1-PCT4;
Do j=1 To dim(Var);
var(j)=(X&mj.-X&mj-1)/X&mj. ;
END;
Run;

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 10 replies
  • 1097 views
  • 2 likes
  • 6 in conversation