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

Hi,


Really stumped by this one (so probably something really obvious that I'm missing).

 

I'm trying to develop a macro with nested DO loops to cycle through all possible combinations of two sets of character variables and 3 sets of numeric variables. The full code is quite lengthy but I'm hoping what I show here will contain the relevant parts.

 

To cycle through my list of character variables I have coded the following (the first variable has two categories, the second has four): -

 

%let h_lst = HA NH;
%let start=%sysfunc(indexc(%sysfunc(compress(&h_lst)),&beg));
%let finish=%sysfunc(indexc(%sysfunc(compress(&h_lst)),&end));


    %let g_lst = AW CHASE FLAT HURDLE;
    %let start1=%sysfunc(indexc(%sysfunc(compress(&g_lst)),&beg1));
    %let finish1=%sysfunc(indexc(%sysfunc(compress(&g_lst)),&end1));
    
%do h = &start %to &finish;    
    %do g = &start1 %to &finish1;
        %do i=9 %to 9;
            %do j=9 %to 9;
                %do k=28 %to 30;

 

I call the macro using keyword parameters: -

 

%macro super_macro
    (beg=HA,
     end=NH,
     beg1=AW,
     end1=HURDLE);

 

When I run the macro, it cycles through all my required variables apart from those in h_lst. The log tells me that although SAS is identifying the list has a BEG of HA and an END of NH, that part of the loop has a start and end value of 1, where I would expect the start value to be 1 and the end value to be 2. The exact same code method used for g_lst works fine and has a start value of 1 and end value of 4. The log for this section is below: -

 

MLOGIC(SUPER_MACRO): %LET (variable name is H_LST)
MLOGIC(SUPER_MACRO): %LET (variable name is START)
SYMBOLGEN: Macro variable H_LST resolves to HA NH
SYMBOLGEN: Macro variable BEG resolves to HA
MLOGIC(SUPER_MACRO): %LET (variable name is FINISH)
SYMBOLGEN: Macro variable H_LST resolves to HA NH
SYMBOLGEN: Macro variable END resolves to NH
MLOGIC(SUPER_MACRO): %LET (variable name is G_LST)
MLOGIC(SUPER_MACRO): %LET (variable name is START1)
SYMBOLGEN: Macro variable G_LST resolves to AW CHASE FLAT HURDLE
SYMBOLGEN: Macro variable BEG1 resolves to AW
MLOGIC(SUPER_MACRO): %LET (variable name is FINISH1)
SYMBOLGEN: Macro variable G_LST resolves to AW CHASE FLAT HURDLE
SYMBOLGEN: Macro variable END1 resolves to HURDLE
SYMBOLGEN: Macro variable START resolves to 1
SYMBOLGEN: Macro variable FINISH resolves to 1
MLOGIC(SUPER_MACRO): %DO loop beginning; index variable H; start value is 1; stop value is 1; by value is 1.
SYMBOLGEN: Macro variable START1 resolves to 1
SYMBOLGEN: Macro variable FINISH1 resolves to 4
MLOGIC(SUPER_MACRO): %DO loop beginning; index variable G; start value is 1; stop value is 4; by value is 1.

 

As I say, it's probably something really obvious that I'm missing but I just can't figure it out so if anyone could point me in the right direction, I'd be hugely appreciative.

 

Many thanks,

Rob

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @robulon,

 

You are using the wrong function: INDEXC searches for single characters from the list in the second argument. So, it finds the "H" from list "NH" in the first position of the string "HANH", hence the result.

 

Consider using FIND, INDEX, FINDW, INDEXW or SCAN (for the latter three your application of COMPRESS is counterproductive) or better, since you're working with macro variables, %INDEX or %SCAN. Even FIND, INDEX and %INDEX could be impaired by COMPRESSing (depending on input data), although less than %SCAN.

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hi @robulon,

 

You are using the wrong function: INDEXC searches for single characters from the list in the second argument. So, it finds the "H" from list "NH" in the first position of the string "HANH", hence the result.

 

Consider using FIND, INDEX, FINDW, INDEXW or SCAN (for the latter three your application of COMPRESS is counterproductive) or better, since you're working with macro variables, %INDEX or %SCAN. Even FIND, INDEX and %INDEX could be impaired by COMPRESSing (depending on input data), although less than %SCAN.

robulon
Quartz | Level 8

Of course! That is brilliant, thanks so much for this.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1016 views
  • 0 likes
  • 2 in conversation