BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Rebecca_K
Fluorite | Level 6

Hello!

 

  I am trying to create an array, where the data has alpha-numeric values.  I keep getting the following error message: "ERROR 22-322: Syntax error, expecting one of the following: a name, (, -, :, ;, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_. " 

Any help is much appreciated!

 

DATA TEST_ARRAY;
INPUT FIELD_NAME : $20.;
CARDS;
A1C
AAA
222;
run;

proc sql ;
   SELECT  FIELD_NAME into 
	      :FIELD_NAME_List separated by ' '
   FROM TEST_ARRAY;
;quit;

 data SomeTable;
    array FieldArray{*} _CHARACTER_ &FIELD_NAME_List.;    /* HERE IS WHERE IT FAILS */
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If you want to place those values into variables in an array:

DATA TEST_ARRAY;
INPUT FIELD_NAME : $20.;
CARDS;
A1C
AAA
222
;
run;

proc sql ;
   SELECT  quote(strip(FIELD_NAME)) into 
	      :FIELD_NAME_List separated by ','
   FROM TEST_ARRAY;
;quit;
%let listsize=&sqlobs;

 data SomeTable;
    array FieldArray{&listsize} $ 20 (&FIELD_NAME_List.);    /* HERE IS WHERE IT FAILS */
run;

&Sqlobs is an automatic variable that holds the number of items returned by the most recent SQL query.

 

To place values into arrays you can use a comma delimited list. Since these values are character they also need to be quoted so do such in the SQL that selects them. A character array needs to have a size associated. I reused the length of the variables in the first data step. You need more code if this is supposed to guess the size of the elements in the array as well.

 

View solution in original post

5 REPLIES 5
SASKiwi
PROC Star

222 isn't a valid name for a SAS variable, so that is most likely the reason for your error. You could just add an alpha character on the front like V222 or any other valid name of your choosing.

ballardw
Super User

If you want to place those values into variables in an array:

DATA TEST_ARRAY;
INPUT FIELD_NAME : $20.;
CARDS;
A1C
AAA
222
;
run;

proc sql ;
   SELECT  quote(strip(FIELD_NAME)) into 
	      :FIELD_NAME_List separated by ','
   FROM TEST_ARRAY;
;quit;
%let listsize=&sqlobs;

 data SomeTable;
    array FieldArray{&listsize} $ 20 (&FIELD_NAME_List.);    /* HERE IS WHERE IT FAILS */
run;

&Sqlobs is an automatic variable that holds the number of items returned by the most recent SQL query.

 

To place values into arrays you can use a comma delimited list. Since these values are character they also need to be quoted so do such in the SQL that selects them. A character array needs to have a size associated. I reused the length of the variables in the first data step. You need more code if this is supposed to guess the size of the elements in the array as well.

 

Tom
Super User Tom
Super User

Remember that SAS does not care if you use spaces or commas in the list of values in the ARRAY statement.  (Same is true for the list passed to the IN operator.)  Spaces work much better in macro variables since you can pass the value to macro functions or other macros.

 

Also using STRIP() instead of TRIM() will remove any leading spaces in the value.  That could make the values not match what was in the original dataset.

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1693 views
  • 1 like
  • 4 in conversation