BookmarkSubscribeRSS Feed
lady8506
Quartz | Level 8

I have this dataset with over 50 variables all starting with the same prefix (cares_sf_1, cares_sf_2, cares_sf_3, cares_sf_4 ,and so on up to 50). I am trying to do create an array that contains all these variables without having to type each one of them out. Is there a way to do this? I thought 'cares_sf_1-cares_sf_7" might work, but apparently it doesn't appear to be the right syntax and I can't find a solution through google searches.

 

Here is what I've currently done that isn't working:

 

DATA CARES; SET RedcapDB;
ARRAY acares_sf_{7} cares_sf_1-cares_sf_7;
DO i = 1 to 7;
acares_sf_[i] = cares_sf_[i] - 1;
END;
RUN;

Error message:

 

7120  DATA CARES; SET RedcapDB;
7121  ARRAY acares_sf_{7} cares_sf_1-cares_sf_7;
7122  DO i = 1 to 7;
7123  acares_sf_[i] = cares_sf_[i] - 1;
ERROR: Undeclared array referenced: cares_sf_.
ERROR: Variable cares_sf_ has not been declared as an array.
7124  END;
7125  RUN;

Any help is appreciated. Thank you!

4 REPLIES 4
ballardw
Super User

You need to declare two arrays acares and cares, OR you meant acares but typed cares.

 

Your list approach should work just fine if the array statement is correct.

 

 

lady8506
Quartz | Level 8
Hey Ballard, I just figured out what I was doing wrong. Thanks for replying.
Reeza
Super User

You're looking for something called variable lists. 

 

Another option is the colon short cut, which includes everything with that prefix:

 

array myArray(*) prefix: ;
Astounding
PROC Star

Assuming you fixed the array name reference, you will also need to change the DO loop:  do i=2 to 7;

 

There is no 0th element to copy into the 1st element.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 832 views
  • 3 likes
  • 4 in conversation