Arrays

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Arrays

Hi, I am a beginner to SAS. i am trying to use array & do loop can someone help me out if there is an error in the following code as every time i try to submit the sas gets stuck    

data model.Vacsick1;

set model.Vacsick (firstobs=1 obs=12);

array VS [12] VS1 - VS12;

Do i = 1 to 12;

if site = 'New York' then

i = i/7;

else i = i/8;

End;

run;

Thanks

Parwaiz


Accepted Solutions
Solution
‎03-23-2014 05:53 PM
Super User
Posts: 19,789

Re: Arrays

Parwaiz wrote:

Hi, I am a beginner to SAS. i am trying to use array & do loop can someone help me out if there is an error in the following code as every time i try to submit the sas gets stuck   

data model.Vacsick1;

set model.Vacsick (firstobs=1 obs=12);

array VS [12] VS1 - VS12;

Do i = 1 to 12;

if site = 'New York' then

i = i/7;

else i = i/8;

End;

run;

Thanks

Parwaiz

An array in SAS is different than other languages.

By Default SAS process data row by row.

Arrays are simply references to multiple variables, so in this case VS1-VS12 will represent Jan2013-Dec2013 or something along those lines.

It's better to set the denominator once, because it doesn't change per row and then apply your division, which I have a feeling isn't what you want.

data model.Vacsick1;

set model.Vacsick (firstobs=1 obs=12);

array VS [12] VS1 - VS12;

if site='New York' then denom=7;

else denom=8;

Do i = 1 to 12;

vs(i)= vs(i)/denom;

End;

run;

View solution in original post


All Replies
PROC Star
Posts: 7,471

Re: Arrays

You have to tell the forum more.  Providing an example of your data would help.

You declare an array, but then never refer to it.  Then, you set up a 12 iteration loop, but do the same thing 12 times.

Plus, you say SAS gets stuck, but you didn't tell the forum about the feedback (i.e., log) it may have provided.

Occasional Contributor
Posts: 8

Re: Arrays

the screen gets stuck in submitting the code, so may be the code doesnt reach the log (not sure).  Below is the image of my raw data. where in i wanted to divide first 3 obs by 7(site New York) and rest by 8 and only for 2013

Image.png

Super User
Posts: 5,503

Re: Arrays

You have created an infinite loop by changing the value of i.  How is i ever supposed to get to 12 when you are dividing its value by 7 or 8 each time through the loop?

Occasional Contributor
Posts: 8

Re: Arrays

Posted in reply to Astounding

ooh.. how do i refer each value then apply the divide function?

Super User
Posts: 5,503

Re: Arrays

You could use a separate variable, along these lines:

if site='New York' then j=i/7;

else j=i/8;

Occasional Contributor
Posts: 8

Re: Arrays

Posted in reply to Astounding

this did work but the values returned in data set are periods with new variables, i would like it to replace the existing variables with processed numbers and retaining the old names for variables...

Image 1.png

Super User
Posts: 19,789

Re: Arrays

Then you've probably declared your array incorrectly.

Do you have initial variables VS1-VS12? If not, replace those with the actual variables of interest in your array statement.

Occasional Contributor
Posts: 8

Re: Arrays

ok i replaced them with actual var names, but the new data set is not giving the processed numbers and has 2 new variables i (values as 13) & j (1.5) which would be the case.

data model.Vacsick1;

set model.Vacsick (firstobs=1 obs=12);

array VS [12] Jan_13 Feb_13 Mar_13 Apr_13 May_13 Jun_13 Jul_13 Aug_13 Sep_13 Oct_13 Nov_13 Dec_13;

Do i = 1 to 12;

if site = 'New York' then

j = i/7;

else j = i/8;

End;

run;

Super User
Posts: 5,503

Re: Arrays

Well, you haven't really stated your intention here but it seems like Reeza has the right idea.  Just use the proper variable names in Reeza's program.

Super User
Posts: 5,503

Re: Arrays

If your final values for VS1-VS12 are missing, there are two possibilities.  Either the program you wrote changed them to missing, or else they were missing before your DATA step began.  If you were to post the program you ended up using, it would probably be clear which possibility you are observing.  You could also examine the data beforehand to rule out one of the possibilities.

Occasional Contributor
Posts: 8

Re: Arrays

Posted in reply to Astounding

i have changed the var names to original ones, the values are not missing inn parent dataset.. below is the code i have now

data model.Vacsick1;

set model.Vacsick (firstobs=1 obs=12);

array VS [12] Jan_13 Feb_13 Mar_13 Apr_13 May_13 Jun_13 Jul_13 Aug_13 Sep_13 Oct_13 Nov_13 Dec_13;

Do i = 1 to 12;

if site = 'New York' then

j = i/7;

else j = i/8;

End;

run;

now there are no missing values but the resultant data set is giving the same old numbers instead of processed..

Solution
‎03-23-2014 05:53 PM
Super User
Posts: 19,789

Re: Arrays

Parwaiz wrote:

Hi, I am a beginner to SAS. i am trying to use array & do loop can someone help me out if there is an error in the following code as every time i try to submit the sas gets stuck   

data model.Vacsick1;

set model.Vacsick (firstobs=1 obs=12);

array VS [12] VS1 - VS12;

Do i = 1 to 12;

if site = 'New York' then

i = i/7;

else i = i/8;

End;

run;

Thanks

Parwaiz

An array in SAS is different than other languages.

By Default SAS process data row by row.

Arrays are simply references to multiple variables, so in this case VS1-VS12 will represent Jan2013-Dec2013 or something along those lines.

It's better to set the denominator once, because it doesn't change per row and then apply your division, which I have a feeling isn't what you want.

data model.Vacsick1;

set model.Vacsick (firstobs=1 obs=12);

array VS [12] VS1 - VS12;

if site='New York' then denom=7;

else denom=8;

Do i = 1 to 12;

vs(i)= vs(i)/denom;

End;

run;

Occasional Contributor
Posts: 8

Re: Arrays

thanks Reeza, Astounding and Arthur for your help this did work & i have the desired dataset now Smiley Happy !!

data model.Vacsick1;

set model.Vacsick (firstobs=1 obs=12);

array VS [12] Jan_13 Feb_13 Mar_13 Apr_13 May_13 Jun_13 Jul_13 Aug_13 Sep_13 Oct_13 Nov_13 Dec_13;

if site = 'New York' then denom=7;

else denom=8;

Do i = 1 to 12;

vs(i)=vs(i)/denom;

End;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 411 views
  • 6 likes
  • 4 in conversation