SAS Array creation

sas character array example
sas create array from dataset
sas array example ucla
sas sum(of array-name * )
array in sas listen data
sas array retain
sas multidimensional array
sas macro array

I am trying to create array that hold a value.

proc sql noprint;
select count(*) into :dscnt from study;
select libname into :libname1 - :libname&dscnt from study;
quit;

I think the syntax is correct but i keep getting this following error message in in SAS studio.

***NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
NOTE: Line generated by the macro variable "DSCNT".
79         libname       4
                      _
                      22
                      200
ERROR 22-322: Syntax error, expecting one of the following: ',', FROM, NOTRIM.  

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

Can someone explain to me what i am doing wrong?

Thanks

You do not need to know the number of items ahead of time, if you leave it blank, SAS will automatically create the correct number of macro variables.

If you do want to use that number elsewhere you can create it using the TRIMMED option to remove any extra spaces. See the second example below.

proc sql noprint;
select name into :name1- from sashelp.class;
quit;

%put &name1;
%put &name19.;

proc sql noprint;
select count(distinct name) into :name_count TRIMMED from sashelp.class;
quit;

%put &name_count;

Results:

3068      proc sql noprint;
3069      select name into :name1- from sashelp.class;
3070      quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds


3071
3072      %put &name1;
Alfred
3073      %put &name19.;
William
3074
3075      proc sql noprint;
3076      select count(distinct name) into :name_count TRIMMED from
3076! sashelp.class;
3077      quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


3078
3079      %put &name_count;
19

[PDF] Using Arrays in SAS Programming, Assigning Initial Values to Array Variables or Elements . Specifying Lower and Upper Bounds of a Temporary Array . Creating a Temporary Array . Using Arrays in SAS® Programming Arrays provide an alternative method of referring to variables. Instead of referring to the first revenue variable as Rev1, you can refer to it by using the array name and an index into the array, such as REVENUE[I] (assuming that I has a value of 1). Using Arrays with Functions and Operators

The into syntax in proc sql stores formatted values into macro variables. For example if you run this code:

proc sql noprint;
  select count(*) into :dscnt from sashelp.class;
quit;

%put #&dscnt#;

You will see the output is:

#      19#

In otherwords the result is left padded with spaces. This means in your example, the code is resolving to something like:

select libname into :libname1 - :libname      19 from study;

^ Which is obviously invalid syntax. To fix this, you can simply add the TRIMMED keyword to your SQL statement:

select count(*) into :dscnt TRIMMED from study;

Thanks to Reeza for the TRIMMED keyword.

ARRAY Statement, statement before a PUT statement, all leading blanks are trimmed. You do not need to know the number of items ahead of time, if you leave it blank, SAS will automatically create the correct number of macro variables. If you do want to use that number elsewhere you can create it using the TRIMMED option to remove any extra spaces. See the second example below.

do something like below

 proc sql noprint;
 select count(*) into :dscnt from sashelp.class;
  select name into :name1 - :name%left(&dscnt) from sashelp.class;
  quit;

158-2010: How to Use Arrays and DO Loops: Do I DO , Example 2 – Creating New Variables with an Array. The PRICEDATA dataset in the SASHELP library contains simulated data of the prices of 17 different  In SAS an array is declared by using the following syntax − ARRAY ARRAY-NAME(SUBSCRIPT) ($) VARIABLE-LIST ARRAY-VALUES In the above syntax − ARRAY is the SAS keyword to declare an array.

Using Formats, Syntax. In SAS an array is declared by using the following syntax − ARRAY ARRAY-NAME(SUBSCRIPT) ($) VARIABLE-LIST ARRAY-VALUES. ​The simplest form of SAS arrays are a one-dimensional arrays. In one-dimension arrays, a grouping of SAS variables is grouped under a single array. Once variables are grouped under a single array, you can easily perform the same calculation on all the variables with just a few lines of code.

C Arrays (With Examples), Arrays are used in the SAS DATA STEP to simplify programming involving a series of related values. The general format for an ARRAY statement is. ARRAY array-  Re: Creating Array from the SAS Dataset Posted 12-06-2017 (6295 views) | In reply to tapas_16880 Quite simply you need to provide test data, in the form of a dataset, using the code window (its the {i} above the post).

The Complete Guide to SAS Arrays, A SAS array is a collection of SAS variables that can be referenced in the applied to all the product price variables creating a new total variable. The initial​  SAS ARRAYS A SAS ARRAY is a set of variables of the same type, called “elements” of the arry, that you want to perform the same operation on. An array name is assigned to the set of variables.

Comments
  • You don't have to pre-count, but if you do remove the leading spaces. See this related answer stackoverflow.com/a/32525294/4965549
  • SAS will count the number of observations returned by the first query for you. If you need the count just save &sqlobs. stackoverflow.com/a/32525294/4965549
  • True, I was trying to demonstrate the TRIMMED option though, how to remove those extra spaces in the first place, which is nice to know for the future.
  • TRIMMED + you don't need to specify the last prefix ever.
  • @Reeza Good tips, do you know if dropping the last prefix is only good for certain versions of SAS or has it been around a long time?
  • I think it was introduced in either 9.3 or 9.4 so relatively recently.