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

I'm using proc nlmixed which I understand doesn't have a class statement, so I need to manually create dummy variables. How would I use an array to create dummies Y2009-Y2022 for existing variable "year" with values 2009-2022? I tried adapting code on p. 9 <https://support.sas.com/resources/papers/97529_Using_Arrays_in_SAS_Programming.pdf>, but it's not working:

array years[009:2002] Y2009-2022;

  do year=2009 to 2022;

 years(year)=0;

end;

years(year)=1;

 

Thanks for any feedback!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Don't use your existing YEAR variable as the DO loop index variable.

Don't use the DO loop just to initialize the values to zero.  Use it to actually set the true/false dummy variables by comparing the current index to the actual YEAR variable.

array years[2009:2022] Y2009-Y2022;
do index=2009 to 2022;
  years[index] = index=year;
end;
drop index;

 

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

When code "is not working", we need to see the log, if there are errors in the log; or if you aren't getting the desired output, then we need to see the output you are getting and an explanation of why it is not correct. Saying "it's not working" and not telling us more information leaves us in the dark.

 

As a guess, you appear to have two typographical errors here:

 

array years[009:2002] Y2009-2022;

 

I leave it up to you, as a homework assignment, to identify and fix the typos. If fixing these still doesn't work, provide the information I requested in the first paragraph.

--
Paige Miller
amyip
Obsidian | Level 7
Yes, absolutely typos -- using SAS EG on a secure server so can't copy and paste, have to retype everything. Should have been:
array years[2009:2002] Y2009-Y2022;
Thanks for the feedback
Kurt_Bremser
Super User

@amyip wrote:

I'm using proc nlmixed which I understand doesn't have a class statement, so I need to manually create dummy variables. How would I use an array to create dummies Y2009-Y2022 for existing variable "year" with values 2009-2022? I tried adapting code on p. 9 <https://support.sas.com/resources/papers/97529_Using_Arrays_in_SAS_Programming.pdf>, but it's not working:

array years[009:2002] Y2009-2022;

  do year=2009 to 2022;

 years(year)=0;

end;

years(year)=1;

 

Thanks for any feedback!


The variable list in your ARRAY statement can't work, as 2022 is not  a valid SAS name. See the ERROR message in the log.

Next, an index from 9 (009) to 2022 creates much more elements than a variable list from 2009 to 2022 will provide. You obviously have to start with 2009.

Next, be consistent in how you work with arrays; don't use normal parentheses, use angular or curly brackets everywhere. Normal brackets should be left for function calls. This makes the code readable, as it is now it is not.

Fourth, after the DO loop, year will always have a value of 2023, which will be outside the range of your array. This will also become obvious by reading the log, once the other mistakes are fixed.

 

If year is a variable coming from a dataset, don't use it in the DO loop, use a new variable which you later drop.

amyip
Obsidian | Level 7
Those were typos from retyping quickly. Should have been:
array years[2009:2002] Y2009-Y2022;
Thanks for your feedback about the brackets -- I did find that confusing in the articles/blogs I was reading.
amyip
Obsidian | Level 7
Very helpful to know about DO loop ending with value of 2023 -- I did see that in the log.
Kurt_Bremser
Super User

An iterative DO loop actually works like this:

  1. set iterator to initial value
  2. check if iterator is past end value, if yes, go to 6.
  3. body of the loop
  4. increment the iterator
  5. go to 2
  6.  

Or in SAS code, a

do i = 1 to 3;

looks like this:

i = 1;
do while (i le 3);
  /* body */
  i + 1;
end;

So you can see that the iterator has to always go past the end. All FOR statements in other languages work the same way and end with the same iterator value.

amyip
Obsidian | Level 7
Thank you for the thoughtful explanation
Tom
Super User Tom
Super User

Don't use your existing YEAR variable as the DO loop index variable.

Don't use the DO loop just to initialize the values to zero.  Use it to actually set the true/false dummy variables by comparing the current index to the actual YEAR variable.

array years[2009:2022] Y2009-Y2022;
do index=2009 to 2022;
  years[index] = index=year;
end;
drop index;

 

amyip
Obsidian | Level 7
Thank you so much -- this code worked perfectly! so much more elegant than 14 if-then-else statements.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 960 views
  • 5 likes
  • 5 in conversation