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!
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;
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.
@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.
An iterative DO loop actually works like this:
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.
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;
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!
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.