DATA Step, Macro, Functions and more

Array when dimension is a macro value

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

Array when dimension is a macro value

Hi all, 

 

could you help me out with this simple problem? I'm trying to write something like below, output diagnosis into diferent variables based on the position of the string given the delimiter ", ".

 

data dementia2;
   set dementia;
ICD9_LIST_1 = scan(CURRENT_ICD9_LIST,1,', ');
ICD9_LIST_2 = scan(CURRENT_ICD9_LIST,2,', ');
ICD9_LIST_3 = scan(CURRENT_ICD9_LIST,3,', ');
ICD9_LIST_4 = scan(CURRENT_ICD9_LIST,4,', ');
ICD9_LIST_5 = scan(CURRENT_ICD9_LIST,5,', ');
run;

 

*****

I want to write the code above as an array, and I would have the dimention of the array as a macro value as below:

 

%put &ICD9_delimiter_cnt.; /*would receive the value of 5*/
 

data dementia2;
   set dementia;
array ICD9_LIST_ARRAY {&ICD9_delimiter_cnt.} ICD9_LIST_1-ICD9_LIST_%left(&ICD9_delimiter_cnt.);

do i = 1 to &ICD9_delimiter_cnt.;
   ICD9_LIST_ARRAY_{i} = scan(CURRENT_ICD9_LIST,{i},', ');
end;

run;

 

**********************

 

I'm getting a number of errors given the array code written above. What am I missing? Your assistance would be appreciated it.

 

Thank you! 


Accepted Solutions
Solution
‎10-24-2016 12:51 PM
Super User
Posts: 5,497

Re: Array when dimension is a macro value

Inside the SCAN function, remove the curly brackets around {i}.

View solution in original post


All Replies
Respected Advisor
Posts: 4,919

Re: Array when dimension is a macro value

The statement 

 

%put &ICD9_delimiter_cnt.;

 

simply writes the value of macro variable ICD9_delimiter_cnt to the log. To assign a value you need

 

%let ICD9_delimiter_cnt = 5;

PG
Contributor
Posts: 45

Re: Array when dimension is a macro value

Hi PG, 

thanks for the reply.

 

yeah the 5 is coming from here:

 

PROC SQL NOPRINT;
SELECT max(ICD9_delimiter_cnt) as ICD9_delimiter_cnt
INTO :ICD9_delimiter_cnt 
FROM delimiter_cnt;
QUIT;


%put &ICD9_delimiter_cnt.;

***

 

So I'm missing something on the data setp that is defining the array.

Super User
Posts: 11,343

Re: Array when dimension is a macro value

[ Edited ]

Generally use of macro functions in data step aren't going to work quite as expected.

But good news If the variables ICD9_LIST1 to nn do not already exist in your data. The array statement will create them without explicit variable names.

 

Array ICD9_LIST {&ICD9_delimiter_cnt};

should suffice, just reference ICD9_List instead of ICD9_List_Array.

Bad news though is if you need Character values then you need to specify the length of the character values in the array declaration.

So you may want

Array ICD9_LIST {&ICD9_delimiter_cnt} $10;

if you expect the length of the longest value to be 10 characters.

 

 

In the future please post the code and messages from the log when you are getting errors. It helps possibly address all of the issues at one time. And post code and/or log into the entry box using the "run" icon to preserve formatting. The Error messages with underscores point out where SAS thinks the error begins and is actually quite helpful.

 

Super User
Posts: 5,497

Re: Array when dimension is a macro value

Other things to look at ...

 

You actually changed the array name.  On the definition, compared to when you reference the array later, you added an underscore to the end of the name.  Whatever you choose has to match.

 

The %LEFT function has the potential to cause trouble.  An easier solution would be to add this statement:

 

%let ICD_delimiter_cnt = &ICD_delimiter_cnt;

 

The %LET statement will ignore any leading blanks, re-assigning a value with no leading or trailing blanks and allowing you to remove the %LEFT function.

Contributor
Posts: 45

Re: Array when dimension is a macro value

Posted in reply to Astounding

Hi all,

 

this is what I'm getting, coming from the log:

 

 

******

PROC SQL NOPRINT;
SELECT max(ICD9_delimiter_cnt) as ICD9_delimiter_cnt
INTO :ICD9_delimiter_cnt
FROM delimiter_cnt;
QUIT;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

 

/*confirming maximum values in the log*/
%put &ICD9_delimiter_cnt.;
5

 

data dementia2;
set dementia;
array ICD9_LIST {&ICD9_delimiter_cnt.} $10;

do i = 1 to &ICD9_delimiter_cnt.;
ICD9_LIST{i} = scan(CURRENT_ICD9_LIST,{i},', ');
-
22
ERROR: Undeclared array referenced: NAME.
ERROR: Variable NAME has not been declared as an array.
ERROR 22-322: Syntax error, expecting one of the following: a name, INPUT,
PUT.

end;
run;

Solution
‎10-24-2016 12:51 PM
Super User
Posts: 5,497

Re: Array when dimension is a macro value

Inside the SCAN function, remove the curly brackets around {i}.

Contributor
Posts: 45

Re: Array when dimension is a macro value

Posted in reply to Astounding
Thanks, Astounding. That was the error on that array code. I appreciate the help! thank you!
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 331 views
  • 0 likes
  • 4 in conversation