DATA Step, Macro, Functions and more

Referencing Global Arrays in Data Step

Reply
Occasional Contributor
Posts: 17

Referencing Global Arrays in Data Step

[ Edited ]

All, I am trying to reference a global array so that I can output a series of files based on the matching of values in a variable. Below is the code:

 

%let topMetros = array[10] $5. metro1-metro10 ('35620' '31090' '33100' '16980' '41860' '47900' '26420' '19100' '41940' '14460'); 

%macro count(toomany); 

%do i = 1 %to &toomany; 

data geog.uscis_ois_lpr_s5_mtr&i.; 
f CBSA_CODE = topMetros[&i]. then output geog.uscis_ois_lpr_s5_mtr&i.; 

%end; 
%mend count; 

%count(10); 

run; 

 

 

When I do I get an error message of the following:

"180: LINE and COLUMN cannot be determined. NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred. ERROR 180-322: Statement is not valid or it is used out of proper order."

Respected Advisor
Posts: 4,919

Re: Referencing Global Arrays in Data Step

Posted in reply to RedMcCallen

Assuming input dataset is called HAVE, I think you mean:

 

%let topMetros = array topMetros[10] $5 metro1-metro10 ('35620' '31090' '33100' '16980' '41860' '47900' '26420' '19100' '41940' '14460'); 

%macro count(toomany); 
%do i = 1 %to &toomany; 
	data geog.uscis_ois_lpr_s5_mtr&i.; 
        set HAVE;
	%topMetros;
	if CBSA_CODE = topMetros[&i.] then output; 
        drop metro:;
	run;
	%end; 
%mend count; 

%count(10);
PG
Occasional Contributor
Posts: 17

Re: Referencing Global Arrays in Data Step

I can't get the formatting to come out right. All I get is this tiny box to put anything into regardless of browser that I use.

However, the above code does not work, keep saying that I have an undeclared array referenced: topMetros.
Respected Advisor
Posts: 4,919

Re: Referencing Global Arrays in Data Step

Posted in reply to RedMcCallen

OK, a tested version now:

 

data have;
do CBSA_CODE = '35620', '31090', '33100', '16980', '41860', '47900', '26420', '19100', '41940', '14460';
output;
end;
run;


options mprint;
%let topMetros = array topMetros[10] $5 metro1-metro10 ('35620' '31090' '33100' '16980' '41860' '47900' '26420' '19100' '41940' '14460'); 

%macro count(toomany); 
%do i = 1 %to &toomany; 
	data uscis_ois_lpr_s5_mtr&i.; 
        set HAVE;
	&topMetros;
	if CBSA_CODE = topMetros[&i.] then output; 
        drop metro:;
	run;
	%end; 
%mend count; 

%count(10);

Replaced %topMetros (macro call) with &topMetros (macro variable reference).

 

PG
Occasional Contributor
Posts: 17

Re: Referencing Global Arrays in Data Step

Nope, now get the following error message.

NOTE: Line generated by the macro variable "TOPMETROS".
38 array[10] $5. metro1-metro10 ('35620' '31090' '33100' '16980' '41860' '47900' '26420' '19100' '41940' '14460')
___
22
200
ERROR: Undeclared array referenced: array.
NOTE: Line generated by the macro variable "TOPMETROS".
38 array[10] $5. metro1-metro10 ('35620' '31090' '33100' '16980' '41860' '47900' '26420' '19100' '41940' '14460')
_______
68
ERROR 22-322: Syntax error, expecting one of the following: +, =.

ERROR 200-322: The symbol is not recognized and will be ignored.

ERROR 68-185: The function METRO10 is unknown, or cannot be accessed.
Respected Advisor
Posts: 4,919

Re: Referencing Global Arrays in Data Step

Posted in reply to RedMcCallen

The code as posted is tested and it works. What did you try?

PG
Occasional Contributor
Posts: 17

Re: Referencing Global Arrays in Data Step

22 GOPTIONS ACCESSIBLE;
23 %let topMetros = array[10] $5 metro1-metro10 ('35620' '31090' '33100' '16980' '41860' '47900' '26420' '19100' '41940'
23 ! '14460');
24
25 %macro count(toomany);
26 %do i = 1 %to &toomany;
27 data geog.uscis_ois_lpr_s5_mtr&i. (drop = metroSmiley Happy;
28 set geog.uscis_ois_lpr_s5_metros;
29 &topMetros;
30
31 if CBSA_CODE = topMetros[&i.] then
32 output geog.uscis_ois_lpr_s5_mtr&i.;
33
34 run;
35
36 %end;
37 %mend count;
38 %count(10);
ERROR: Undeclared array referenced: array.
NOTE 138-205: Line generated by the macro variable "TOPMETROS".
38 array[10] $5 metro1-metro10 ('35620' '31090' '33100' '16980' '41860' '47900' '26420' '19100' '41940' '14460')
_
22
ERROR 22-322: Syntax error, expecting one of the following: +, =.

NOTE: Line generated by the macro variable "TOPMETROS".
38 array[10] $5 metro1-metro10 ('35620' '31090' '33100' '16980' '41860' '47900' '26420' '19100' '41940' '14460')
_
200
ERROR 200-322: The symbol is not recognized and will be ignored.

NOTE: Line generated by the macro variable "TOPMETROS".
38 array[10] $5 metro1-metro10 ('35620' '31090' '33100' '16980' '41860' '47900' '26420' '19100' '41940' '14460')
_______
68
ERROR 68-185: The function METRO10 is unknown, or cannot be accessed.
2 The SAS System 15:19 Monday, November 14, 2016


NOTE: Line generated by the macro variable "TOPMETROS".
38 array[10] $5 metro1-metro10 ('35620' '31090' '33100' '16980' '41860' '47900' '26420' '19100' '41940' '14460')
_______
388
ERROR 388-185: Expecting an arithmetic operator.

NOTE: Line generated by the macro variable "TOPMETROS".
38 array[10] $5 metro1-metro10 ('35620' '31090' '33100' '16980' '41860' '47900' '26420' '19100' '41940' '14460')
_______
76
ERROR 76-322: Syntax error, statement will be ignored.

ERROR: Undeclared array referenced: topMetros.
ERROR: Variable topMetros has not been declared as an array.

NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn).
3537:113
WARNING: The variable 'metro:'n in the DROP, KEEP, or RENAME list has never been referenced.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set GEOG.USCIS_OIS_LPR_S5_MTR1 may be incomplete. When this step was stopped there were 0 observations and 36
variables.
WARNING: Data set GEOG.USCIS_OIS_LPR_S5_MTR1 was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
Respected Advisor
Posts: 4,919

Re: Referencing Global Arrays in Data Step

Posted in reply to RedMcCallen

It is

 

%let topMetros = array topMetros[10] $5 metro1-metro10...

PG
Occasional Contributor
Posts: 17

Re: Referencing Global Arrays in Data Step

Can't believe I missed that! I was focusing on the main bloc. But still, I am an editor and should have caught that.

Thanks.
Ask a Question
Discussion stats
  • 8 replies
  • 760 views
  • 0 likes
  • 2 in conversation