BookmarkSubscribeRSS Feed
Scott86
Obsidian | Level 7

Hi everyone,

 

I have a dataset that I transposed and I want to do an if statement across multiple columns. I have attached a dummy version of the dataset (real once is a little more extensive but same concept). I have pasted my code below that I am trying to use but i get an error (pasted below). The column names in the array will be slightly different.

 

Any help is appreciated. 

 

Thanks everyone

 

ERROR: Array subscript out of range at line 38 column 22.

 

data target;
set trans;

array pre {2} $8 m_202001 m_202002;
array post {1} $8 m_202003;
array time_period {3} $8 m_202001 m_202002 m_202003;

do i=1 to dim(time_period);
	if pre[i] = '' and post[i] = '' then target = 1;
    else if pre[i] ne '' and post[i] = '' then target = 2;
	else if pre[i] = '' and post[i] ne '' then target = 3;
	else target = 4;
end;

run;
2 REPLIES 2
WarrenKuhfeld
Rhodochrosite | Level 12

 Your index, i, goes from 1 to 3 and it indexes arrays with 1 or 2 elements. As you see, that does not work. 

Astounding
PROC Star

Because of the errors, it's impossible to determine what you are trying to accomplish.  Here's a step you can take to eliminate the errors.  But whether it accomplishes the right thing or not ... that's impossible to tell.  Perhaps it will:

 

The current array definitions:

array pre {2} $8 m_202001 m_202002;
array post {1} $8 m_202003;
array time_period {3} $8 m_202001 m_202002 m_202003;

Legally, you could change it to:

array pre {3} $8 m_202001 m_202002 m_202002;
array post {3} $8 m_202003 m_202003 m_202003;
array time_period {3} $8 m_202001 m_202002 m_202003;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 635 views
  • 0 likes
  • 3 in conversation