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

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

View solution in original post

7 REPLIES 7
PGStats
Opal | Level 21

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
avbraga
Calcite | Level 5

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.

ballardw
Super User

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.

 

Astounding
PROC Star

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.

avbraga
Calcite | Level 5

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;

Astounding
PROC Star

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

avbraga
Calcite | Level 5
Thanks, Astounding. That was the error on that array code. I appreciate the help! thank you!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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