BookmarkSubscribeRSS Feed
serenedion
Fluorite | Level 6

Hi Everybody!

I have a question on some data. I am measuring potential (V) at minute intervals, and I want to know the duration (and percentage) of time that this potential is within a certain range. 

 

For example, if the data looked like this:

Time (min)V (uV)
10.229528
20.262015
30.06782
41.263749
52.969982
62.831766
71.749711
80.164038
90.685758
100.924174
110.150946
120.384327
131.135237
141.9148
151.039687

 

How would I calculate the amount of time that the variable V is:

1) < 1 uV

2) in the range 1-2 uV (including 1 and 2 uV)

3) > 2 uV 

 

The sum of these three times should also add to make 15 minutes as well. 

 

I have tried some basic code but I would really appreciate peoples help!

Kind regards,
Dion

5 REPLIES 5
ballardw
Super User

A very powerful tool in SAS is the custom format. You can create ranges that will be used by Reporting or Analysis procedures to make groups. Most of the graphing procedures will honor them as well.

 

Here is an example of creating a data set with data step code (which the preferred way to show data as we can test code against such a data set when we run it). Then create a custom format and demonstrate how Proc Freq will generate the counts, running total, percent of values and running percent.

 

data have;
   input minutes Uv;
datalines;
1	0.229528
2	0.262015
3	0.06782
4	1.263749
5	2.969982
6	2.831766
7	1.749711
8	0.164038
9	0.685758
10	0.924174
11	0.150946
12	0.384327
13	1.135237
14	1.9148
15	1.039687
;

proc format;
value uvrange
low -<1 = 'uV<1'
1 - 2   = 'uV 1 to 2'
2<-high = 'uV>2'
;
proc freq data=have;
  tables uv;
  format uv uvrange.;
run;
data want;
   set have;
   Low = (uv < 1);
   Mid = (1 le uv le 2)l
   high

One of the very powerful advantages of a format is that when the boss asks "what about interval boundaries at 1.5 and 2.5?" or anything similar. The only requirement would the create a new format and use that with the Prod Freq code instead of this Uvrange format.

Note that this works for just about anything where groups ONLY depend on values of a single variable.

 

If your data had another variable, such as the machine or operator that made the recording you could include that variable on the tables statement to get the counts and percentages for each machine or operator (or test sequence or other identifier like this:

proc freq data=have;
  tables uv * operatorid;
  format uv uvrange.;
run;

 

serenedion
Fluorite | Level 6

Thank you for your quick reply ballardw! It is really useful. I see know how you have applied a format to set the variable as low high or medium, and then run a proc freq of this new formatted variable.

Can I just check, what does the last bit of code do? Specifically:

data want;
   set have;
   Low = (uv < 1);
   Mid = (1 le uv le 2)l
   high

Does this code calculate the length of time that uV is in each range (rather than the number of points in the variable uV that are Low, Mid or High)? For this, could I add minutes to proc freq?

ballardw
Super User

@serenedion wrote:

Thank you for your quick reply ballardw! It is really useful. I see know how you have applied a format to set the variable as low high or medium, and then run a proc freq of this new formatted variable.

Can I just check, what does the last bit of code do? Specifically:

data want;
   set have;
   Low = (uv < 1);
   Mid = (1 le uv le 2)l
   high

Does this code calculate the length of time that uV is in each range (rather than the number of points in the variable uV that are Low, Mid or High)? For this, could I add minutes to proc freq?


typo. Started to create new variables. The code would have created 3 variables that were 1/0 coded. Just forgot to remove it.

HB
Barite | Level 11 HB
Barite | Level 11

@serenedion wrote:

 

Does this code calculate the length of time that uV is in each range (rather than the number of points in the variable uV that are Low, Mid or High)? For this, could I add minutes to proc freq?

You indicated each point was a minute.  So 5 points is 5 minutes.

 

Are you also wanting "streaks"?  Your data would have "streaks" of 3,1,2,1,5, and 3 presumeably? 

serenedion
Fluorite | Level 6
Hi HB, thanks for getting back to me. It took a while to work out what you meant!! But yes, I guess that could be a useful metric to report aswell. How would you identify and calculate these different streaks? Could you assign to each streak a duration? The other issue I have is that some of my data points are missing, so how would I handle this?

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