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: -
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
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.
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.
Of course! That is brilliant, thanks so much for this.
Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.
Explore Now →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.