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
Ammonite | Level 13

 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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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