BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
leftygrove
Calcite | Level 5

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

leftygrove
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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