BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Cornelis
Fluorite | Level 6

Hello SAS users,

 

I have a table where I want to mark every observation with events 'Opened' and 'Close'.

Cornelis_0-1708952522838.png

Column Valve describes the position of a valve, where 1 = open and 0 = close.

At 26FEB24:04:52 the valve is opened and column 'Status' must mark this with 'Opened'

At 26FEB24:04:54, the valve is closed and must have the status 'Closed'.

In fact, the valve is opened during 3 minutes.

 

Could you give me a clue how to program/add column Status with Opened/Closed taking into account that the valve will be opened for 1, 2, 3 and more minutes?

 

Thanks and looking forward,

 

Best Regards,

 

Cornelis

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Please provide your sample data in the form of a working DATA step.  The program below is untested in the absence of such sample data:

 

data want;
  set have;
  by valve notsorted;
  if valve=1 then do;
    if first.valve=1 and last.valve=1 then status='Open and Closed';
    else if first.valve=1 then status='Open';
    else if last.valve=1 then status='Closed';
  end;
run;

 

The "notsorted" option of the BY statement is especially useful here, since it allows you to track changes between valve=1 and valve=0, without regard to whether the valve change is positive or negative.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

It is not clear (to me) in which way the Datetime has an influence on the Status variable.

Please not that it will be easier to help you would post the data in usable form.

Cornelis
Fluorite | Level 6

Hello Andreas,

 

Cornelis_0-1708959729762.png

 

Column Status is the resulting output after running a SAS program.

Observation 3: The valve is open and the datetime = 26FEB24:04:52:00.

At  26FEB24:04:54:00, observation 5, the valve is still open, but will be closed of very soon, so the Status is marked as 'Close".

The total time that a valve is open lies between 04:52 - 04:54 hour.

 

Does this make sense?

 

Regards,

 

Cornelis

 

mkeintz
PROC Star

Please provide your sample data in the form of a working DATA step.  The program below is untested in the absence of such sample data:

 

data want;
  set have;
  by valve notsorted;
  if valve=1 then do;
    if first.valve=1 and last.valve=1 then status='Open and Closed';
    else if first.valve=1 then status='Open';
    else if last.valve=1 then status='Closed';
  end;
run;

 

The "notsorted" option of the BY statement is especially useful here, since it allows you to track changes between valve=1 and valve=0, without regard to whether the valve change is positive or negative.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Cornelis
Fluorite | Level 6

Thank you very much for your quick solution!

The not-sorted option do the trick.

Indeed, supplying the SAS code is helpful. For the completeness, here is the SAS code and already validated:


data have;
input valve $1-2 datetime $3-20 ;
cards;
0	26FEB24:04:51:00
1	26FEB24:04:52:00
1	26FEB24:04:53:00
1	26FEB24:04:54:00
0	26FEB24:04:55:00
0	26FEB24:04:56:00
0	26FEB24:04:57:00
1	26FEB24:04:58:00
1	26FEB24:04:59:00
0	26FEB24:05:00:00
0	26FEB24:05:01:00
1	26FEB24:05:02:00
0	26FEB24:05:03:00
0	26FEB24:05:04:00
;
run;



data want;
  set have;
  by valve notsorted;
  if valve=1 then do;
    if first.valve=1 and last.valve=1 then status1='Open and Closed';
    else if first.valve=1 then status1='Open';
    else if last.valve=1 then status1='Closed';
  end;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 4 replies
  • 803 views
  • 1 like
  • 3 in conversation