I'll post a piece of the code just to get my point across. I have a dataset that has multiple VIOLATION variables in the form of VIOLATION1, VIOLATION2, and so forth, up to 10. I'd like to go through these variables and fill in "Name X", "Date X", "Incident X" by referencing the correct Violation X. But in the below example, &Incident_macro shows up as the text incident_counter, instead of showing up as 1 (or 2, or 3, or wherever it is in the loop). Does anyone know a way I could workaround this so the macro shows the numeric incident_counter, not the text? I should mention that the situation is a bit more complicated than this, which is why I can't just forgo the loop process and type out 1,2,3. Thanks for your help!!!
incident_counter = 1;
%let incident_macro = incident_counter;
loop_counter = 0;
do while (loop_counter < 10);
if VIOLATION&incident_macro ne ' ' then do;
'Name &incident_macro'n = VIOLATION&incident_macro;
'Date &incident_macro'n = Date&incident_macro;
'Incident &incident_macro'n = incident_counter;
incident_counter = incident_counter + 1;
%let incident_macro = incident_counter;
end;
loop_counter = loop_counter + 1;
end;
Totally agree with @PaigeMiller here, prime example of the use of arrays that. E.g.
data want; set have; array violation{10}; do i=1 to 10; if violation{i} ne " " then do; <variable>=violation{i}; ... end; end; run;
I would really avoid using 'Name ...'n type naming convention. There is only one real place where you have to use it, when using Excel files directly, or in a database with bad setup. Neither of which should drive your coding. Fix that and use good naming on your variables and you will find your programming life much easier.
You can't mix macro and data step code in this manner (there are ways to do this, but this way doesn't work).
Furthermore, you probably want to use the data step ARRAY feature rather than macros to do this. No macros needed.
Totally agree with @PaigeMiller here, prime example of the use of arrays that. E.g.
data want; set have; array violation{10}; do i=1 to 10; if violation{i} ne " " then do; <variable>=violation{i}; ... end; end; run;
I would really avoid using 'Name ...'n type naming convention. There is only one real place where you have to use it, when using Excel files directly, or in a database with bad setup. Neither of which should drive your coding. Fix that and use good naming on your variables and you will find your programming life much easier.
Thanks folks-- I'm now seeing the light. At first, I felt forced to use those specific names because that's how the output needs to look (this feeds into a piece of software that requires those fixed fields). But now I realize I can just use an array to go through the loop process, and handle the renaming later. Thanks a bunch for your help!!
Yes, you have to have two lines here. The first is what the end user wants, and the second is what you want. How you program is up to you, and you should use the simplest, easiest to read and understand process possible. If the end user wants something daft, then apply that right at the end of the process so that it does not affect your part of the process.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.