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

I have a data set with the variables VentUnit1-VentUnit151,

 

I have been attempting to use arrays to flag observations in which VentUnit(i) in ('8N-ICU, '8S-NSICU') and VentUnit(i+1) in ('8N-ICU, '8S-NSICU') such as Line#4 in the screenshot.  I'm having trouble trying to code this with do loops and have a feeling I'm taking the wrong approach. 

 

 VentUnitScreenShot.JPG

 

Any thoughts?

 

Thanks.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
do i=1 to i=151

This is likely wrong and should  be:

 

do i=1 to dim(unit)-1;

Because you're checking i+1 you can only go up to one less than the dimension and you can use the DIM() function to get the number of elements. 

 

You may also want to set CHECK to 0 at the beginning of the loop but otherwise that seems correct.

 

data lines5;
	set lines4;

	array Unit (*) VentUnit1-VentUnit151;

check=0;

		do i=1 to dim(unit)-1;
			if Unit(i) in ('8N-ICU','8S-NSICU') and Unit(i+1) in ('8N-ICU','8S-NSICU') then check+1;
		end;

run;

 

 

 

View solution in original post

4 REPLIES 4
Reeza
Super User

 I'm having trouble trying to code this with do loops and have a feeling I'm taking the wrong approach. 

 

Can you show us the code you're using?

 


@malmario wrote:

I have a data set with the variables VentUnit1-VentUnit151,

 

I have been attempting to use arrays to flag observations in which VentUnit(i) in ('8N-ICU, '8S-NSICU') and VentUnit(i+1) in ('8N-ICU, '8S-NSICU') such as Line#4 in the screenshot.  I'm having trouble trying to code this with do loops and have a feeling I'm taking the wrong approach. 

 

 VentUnitScreenShot.JPG

 

Any thoughts?

 

Thanks.

 

 


 

malmario
Calcite | Level 5
data lines5;
	set lines4;

	array Unit (*) VentUnit1-VentUnit151;

		do i=1 to i=151;
			if Unit(i) in ('8N-ICU','8S-NSICU') and Unit(i+1) in ('8N-ICU','8S-NSICU') then check+1;
		end;

run;
Reeza
Super User
do i=1 to i=151

This is likely wrong and should  be:

 

do i=1 to dim(unit)-1;

Because you're checking i+1 you can only go up to one less than the dimension and you can use the DIM() function to get the number of elements. 

 

You may also want to set CHECK to 0 at the beginning of the loop but otherwise that seems correct.

 

data lines5;
	set lines4;

	array Unit (*) VentUnit1-VentUnit151;

check=0;

		do i=1 to dim(unit)-1;
			if Unit(i) in ('8N-ICU','8S-NSICU') and Unit(i+1) in ('8N-ICU','8S-NSICU') then check+1;
		end;

run;

 

 

 

malmario
Calcite | Level 5

Thank you Reeza!

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!

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