Selecting values from a macro array

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Selecting values from a macro array

I have several multi-dimensional arrays I need to use in a very large dataset.   Rather than trying to merge them, I thought it might be more efficient to create macro arrays and them selectively assign macro values in the large data set with do loops.   However, I've run into an issue.   The Do Loop is not "1 to X" but rather "2006 to 2014."    %eval will not read my formula (see row 30).   Any suggestions on a better way?

 

26 %let DedV1_list=472 495 516 524 538 540 540 550;

27 Data _null_;

28 Year=2007;

29 %macro rap;

30 %let Ded=%scan(&DedV1_list,%eval(year-2006));

31 %put &DEd;

32 %mend;

33 %rap

SYMBOLGEN: Macro variable DEDV1_LIST resolves to 472 495 516 524 538 540 540 550

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:

year-2006

WARNING: Argument 2 to macro function %SCAN is out of range.

ERROR: The macro RAP will stop executing.

34 run;

 


Accepted Solutions
Solution
‎03-30-2017 10:47 PM
Super User
Super User
Posts: 6,842

Re: Selecting values from a macro array

Not sure you should be putting data into macro variables, but your example looks pretty simple.

You could just use a SCAN() function.

%let DedV1_list=472 495 516 524 538 540 540 550;

data want ;
  set have ;
  ded = input(scan("&dedv1_list",year - 2006),32.);
run;

Or you could make a temporary array in your data step.

data want ;
  array dedv1_list (%sysfunc(countw(&dedv1_list))) _temporary_ (&dedv1_list);
  set have ;
  ded = dedv1_list(year - 2006) ;
run;

View solution in original post


All Replies
Super User
Posts: 19,057

Re: Selecting values from a macro array

You shouldn't embed a macro within a data step. You're mixing data step and macro logic. So...what are you trying to do overall? 

 

I think a temporary array may be a better solution - use the macro variable to create the array if you like. 

 

Array myNum(*) _temporary_ (&devv1_list);  

 

dev1 = myNum(year - 2006);

 

call symputx('dev1', dev1, 'g');

 

run;

 

%put &dev1;

Solution
‎03-30-2017 10:47 PM
Super User
Super User
Posts: 6,842

Re: Selecting values from a macro array

Not sure you should be putting data into macro variables, but your example looks pretty simple.

You could just use a SCAN() function.

%let DedV1_list=472 495 516 524 538 540 540 550;

data want ;
  set have ;
  ded = input(scan("&dedv1_list",year - 2006),32.);
run;

Or you could make a temporary array in your data step.

data want ;
  array dedv1_list (%sysfunc(countw(&dedv1_list))) _temporary_ (&dedv1_list);
  set have ;
  ded = dedv1_list(year - 2006) ;
run;
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 138 views
  • 0 likes
  • 3 in conversation