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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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;
Patrick
Opal | Level 21

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.

Patrick_0-1702593539075.png

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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