- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
array years[2009:2002] Y2009-Y2022;
Thanks for the feedback
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
An iterative DO loop actually works like this:
- set iterator to initial value
- check if iterator is past end value, if yes, go to 6.
- body of the loop
- increment the iterator
- go to 2
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content