DATA Step, Macro, Functions and more

Create an array from a macro variable

Reply
Occasional Contributor
Posts: 6

Create an array from a macro variable

[ Edited ]

Hi!

I've been struggling with a minor task. I want to extract numbers from a single column on an Excel and use it in a Risk Dimensions process, and I would like to have an array in which every item is an observation (one of the numbers that I extracted from the Excel file).

 

sfsfsfs.png

I would like to end up with something like: tasas1 = 95.354 tasas 2= 95.272 and so on (not exactly, as I only want the observations corresponding to the 22th of January of July, but that's the idea).

 

So, I got the following code:

 

 

%let path = "/data/user/XX";
libname desa "/data/user/XX";


data AA;
set desa.cer;

if ((month(fecha) = 07 or month(fecha)= 01) and  day(fecha) = 22) then
output;
run;

proc sql noprint;
select cer into :emp_list separated by ' ' from AA;
quit;

data BB;
array tasas [6];
do i = 1 to 6;
tasas[i] =  scan(&emp_list, i, ' ');
i = i+1;
end;
run;

 

I know the macro variable emp_list has all the values I want:

 

ss.png

 

The issue comes when I try to assign each item in the array the value extracted from emp_list. I get: 

 

ERROR 388-185: Expecting an arithmetic operator.

ERROR 200-322: The symbol is not recognized and will be ignored.

 

And when I use other characters and separators (such as |) I get:

 

sccs.png

Is there any way to get it right?

 

Thanks!

 

Occasional Contributor
Posts: 6

Re: Create an array from a macro variable

Is there any reason you need to use the macro variable?

 

Seems to me this would be an excellent use of a transpose statement.

For example:

After the AA data step, use

 

proc transpose data=AA out=BB prefix=tasas;

var CER;

run;

 

This has the added benefit of producing as many or as few columns as there are records in AA.

Super User
Posts: 6,648

Re: Create an array from a macro variable

Loading a list of values into an array is easier than you would think.  You have this code:

 

data BB;
array tasas [6];
do i = 1 to 6;
tasas[i] =  scan(&emp_list, i, ' ');
i = i+1;
end;
run;

 

Try this version:

 

data BB;
array tasas [6] (&emp_list);
run;

 

Occasional Contributor
Posts: 6

Re: Create an array from a macro variable

[ Edited ]
Posted in reply to Astounding

Thanks! It worked, but I came into another problem. It creates the variables tasas1 to tasas6, but when I try to use them in a DO loop I get ERROR: Undeclared array referenced: tasas.
ERROR: Variable tasas has not been declared as an array.

 

I'm kind of lost. Didn't I create an array named tasas?

Super User
Posts: 6,648

Re: Create an array from a macro variable

Array definitions are temporary.  Arrays exist only in the same DATA step that contains the ARRAY statement.  You might need to repeat the ARRAY statement in a later DATA step.

Occasional Contributor
Posts: 6

Re: Create an array from a macro variable

Posted in reply to Astounding

What do I have to do if I want to use those values in a DO Loop inside a proc compile?

 

I can't use macros.

Super User
Posts: 6,648

Re: Create an array from a macro variable

Sorry, I don't think I will be able to help with PROC COMPILE.

Ask a Question
Discussion stats
  • 6 replies
  • 122 views
  • 1 like
  • 3 in conversation