DATA Step, Macro, Functions and more

Trying to use a macro within a loop to adjust multiple variables. Struggling!

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Trying to use a macro within a loop to adjust multiple variables. Struggling!

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;

 


Accepted Solutions
Solution
‎04-12-2018 10:07 AM
Super User
Super User
Posts: 9,427

Re: Trying to use a macro within a loop to adjust multiple variables. Struggling!

Posted in reply to leftygrove

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.

View solution in original post


All Replies
Respected Advisor
Posts: 2,833

Re: Trying to use a macro within a loop to adjust multiple variables. Struggling!

Posted in reply to leftygrove

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.

--
Paige Miller
Solution
‎04-12-2018 10:07 AM
Super User
Super User
Posts: 9,427

Re: Trying to use a macro within a loop to adjust multiple variables. Struggling!

Posted in reply to leftygrove

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.

New Contributor
Posts: 3

Re: Trying to use a macro within a loop to adjust multiple variables. Struggling!

Posted in reply to leftygrove

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!!

Super User
Super User
Posts: 9,427

Re: Trying to use a macro within a loop to adjust multiple variables. Struggling!

Posted in reply to leftygrove

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.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 77 views
  • 5 likes
  • 3 in conversation