I'm learning SAS Arrays. I have the following code:
data work.DublinPrecipRotate; set pg3.weather_dublinmadrid_monthly5yr (keep=City Year PrecipQ1-PrecipQ4); where City='Dublin'; array PrecipQ[*] PrecipQ:; Boundry=dim(PrecipQ); array Quarter[Boundry]; do Q=1 to Boundry; Quarter[q] = PrecipQ[q] * 2.54; end; run;
SAS generates error mesg at:
array Quarter[Boundry];
If I explicitly put the array size:
array Quarter[4];
Then it works. How can I dynamically size Quarter array based on PrecipQ array? Thanks
You don't. It you put a variable name where the size should be SAS thinks you are trying to define the variable to use for implicit indexing into the array, like the way ARRAY statement worked before they introduced explicit indexing.
What is it that you are trying to do?
Why do you have multiple PrecipQ... variables to begin with?
If you have multiple observations instead then your code is simple.
Quarter = PrecipQ * 2.54
To do what you want you will need to first put the count into a macro variable and use the macro variable to generate the constant number that the ARRAY statement wants.
data _null_;
set pg3.weather_dublinmadrid_monthly5yr(obs=1 keep=PrecipQ:);
array PrecipQ PrecipQ:;
call symputx('N',dim(precipq));
run;
data work.DublinPrecipRotate;
set pg3.weather_dublinmadrid_monthly5yr;
where City='Dublin';
array PrecipQ PrecipQ:;
array Quarter[&n];
keep City Year PrecipQ: Quarter1-Quarter&n;
do Q=1 to &n;
Quarter[q] = PrecipQ[q] * 2.54;
end;
run;
You don't. It you put a variable name where the size should be SAS thinks you are trying to define the variable to use for implicit indexing into the array, like the way ARRAY statement worked before they introduced explicit indexing.
What is it that you are trying to do?
Why do you have multiple PrecipQ... variables to begin with?
If you have multiple observations instead then your code is simple.
Quarter = PrecipQ * 2.54
To do what you want you will need to first put the count into a macro variable and use the macro variable to generate the constant number that the ARRAY statement wants.
data _null_;
set pg3.weather_dublinmadrid_monthly5yr(obs=1 keep=PrecipQ:);
array PrecipQ PrecipQ:;
call symputx('N',dim(precipq));
run;
data work.DublinPrecipRotate;
set pg3.weather_dublinmadrid_monthly5yr;
where City='Dublin';
array PrecipQ PrecipQ:;
array Quarter[&n];
keep City Year PrecipQ: Quarter1-Quarter&n;
do Q=1 to &n;
Quarter[q] = PrecipQ[q] * 2.54;
end;
run;
Each data step has a compilation and an execution phase. Overview of DATA Step Processing
The array gets defined during the compilation phase, any value assignment to variables only happens during the execution phase.
Boundry gets only populated during the execution phase and though you can't use it for the array definition that's done in the compilation phase.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.