<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Percentage of time for a numerical variable to assume a value below a given threshold in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Percentage-of-time-for-a-numerical-variable-to-assume-a-value/m-p/471729#M120862</link>
    <description>&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Dear all,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I have a dataset as shown below. ID is the subject ID and each of the variables Var1 and Var2 are measured every minute and can both have missing values.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Dataset HAVE:&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;ID Minutes Var1 Var2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;A 1 -3.2 -5.1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;A 2 . -3.6&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;A 3 . -1.1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;A 4 5.2 -4.9&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;B 1 3.2 1.2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;B 2 4.5 1.2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;B 3 3.1 4.2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;C 1 . 1.9&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;C 2 . -2.2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;C 3 2.1 -3.1&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I would like to count the number of times each ID has a value of Var1 and Var2 below (e.g.) zero. This I would like to output as a new dataset showing for each ID, the period of time (one entry is one minute) each of the two variables had a value below zero, and the percentage/ratio of&amp;nbsp;values below zero to the total number of entries (excluding missing values), e.g. like:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Dataset WANT:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;ID Var1_minutes_below Var2_minutes_below Var1_perc_below Var2_perc_below&lt;BR /&gt;A 2 4 0.5 1&lt;BR /&gt;B 0 0 0 0&lt;BR /&gt;C 0 2 0 0.66&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;So far, I've been using SAS only for simple data management/statistics and this task I can't solve. Have tried&amp;nbsp;reading a lot of posts solving similar problems using SQL/arrays, but I only have experience in the data step. I hope someone can help me with this?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Kind regards,&lt;BR /&gt;Alexander&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 20 Jun 2018 15:23:43 GMT</pubDate>
    <dc:creator>AlexanderL</dc:creator>
    <dc:date>2018-06-20T15:23:43Z</dc:date>
    <item>
      <title>Percentage of time for a numerical variable to assume a value below a given threshold</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Percentage-of-time-for-a-numerical-variable-to-assume-a-value/m-p/471729#M120862</link>
      <description>&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Dear all,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I have a dataset as shown below. ID is the subject ID and each of the variables Var1 and Var2 are measured every minute and can both have missing values.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Dataset HAVE:&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;ID Minutes Var1 Var2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;A 1 -3.2 -5.1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;A 2 . -3.6&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;A 3 . -1.1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;A 4 5.2 -4.9&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;B 1 3.2 1.2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;B 2 4.5 1.2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;B 3 3.1 4.2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;C 1 . 1.9&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;C 2 . -2.2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;C 3 2.1 -3.1&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I would like to count the number of times each ID has a value of Var1 and Var2 below (e.g.) zero. This I would like to output as a new dataset showing for each ID, the period of time (one entry is one minute) each of the two variables had a value below zero, and the percentage/ratio of&amp;nbsp;values below zero to the total number of entries (excluding missing values), e.g. like:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Dataset WANT:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;ID Var1_minutes_below Var2_minutes_below Var1_perc_below Var2_perc_below&lt;BR /&gt;A 2 4 0.5 1&lt;BR /&gt;B 0 0 0 0&lt;BR /&gt;C 0 2 0 0.66&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;So far, I've been using SAS only for simple data management/statistics and this task I can't solve. Have tried&amp;nbsp;reading a lot of posts solving similar problems using SQL/arrays, but I only have experience in the data step. I hope someone can help me with this?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Kind regards,&lt;BR /&gt;Alexander&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jun 2018 15:23:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Percentage-of-time-for-a-numerical-variable-to-assume-a-value/m-p/471729#M120862</guid>
      <dc:creator>AlexanderL</dc:creator>
      <dc:date>2018-06-20T15:23:43Z</dc:date>
    </item>
    <item>
      <title>Re: Percentage of time for a numerical variable to assume a value below a given threshold</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Percentage-of-time-for-a-numerical-variable-to-assume-a-value/m-p/471756#M120869</link>
      <description>&lt;P&gt;One way:&lt;/P&gt;
&lt;P&gt;Please note the data step as the way to provide data that we can test with.&lt;/P&gt;
&lt;PRE&gt;data have;
  input ID $ Minutes Var1 Var2;
  if not missing(var1) then var1_below=  (. &amp;lt; var1 &amp;lt;0)  ;
  if not missing(var2) then    var2_below=  (. &amp;lt; var2 &amp;lt;0)  ;
datalines;
A 1 -3.2 -5.1
A 2 . -3.6
A 3 . -1.1
A 4 5.2 -4.9
B 1 3.2 1.2
B 2 4.5 1.2
B 3 3.1 4.2
C 1 . 1.9
C 2 . -2.2
C 3 2.1 -3.1
;
run;

proc summary data=have nway;
   class id;
   var var1_below var2_below;
   output out=havesum (drop= _:) sum= n= /autoname;
run;

data want;
   set havesum;
   var1_perc_below = var1_below_sum/ var1_below_n;
   var2_perc_below = var2_below_sum/ var2_below_n;
   rename var1_below_sum=var1_below_minutes  
          var2_below_sum=var2_below_minutes 
   ;
   drop var1_below_n var2_below_n;
run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;to calculate a percent you need to have a denominator. I am inferring that you want the number of records.&lt;/P&gt;
&lt;P&gt;I am creating separate _N variables as you may have data at some point where you have&amp;nbsp;values that you may not want to include&amp;nbsp;for the denominator so getting in the habit of identifying each N is good idea.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note if you want a report you could use a different approach with a report procedure&lt;/P&gt;
&lt;PRE&gt;proc tabulate data= have;
   class id;
   var var1_below var2_below;
   table id='',
         (var1_below="Var1 minutes Below" var2_below="Var2 minutes Below")*sum=""*f=best5.
         (var1_below="Var1 % minutes Below" var2_below="Var2 % minutes Below")*mean=""*f=best5.
         /box=id
   ;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Jun 2018 15:51:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Percentage-of-time-for-a-numerical-variable-to-assume-a-value/m-p/471756#M120869</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-20T15:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: Percentage of time for a numerical variable to assume a value below a given threshold</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Percentage-of-time-for-a-numerical-variable-to-assume-a-value/m-p/471757#M120870</link>
      <description>&lt;P&gt;Hi , assuming the missing the value as zero (which is not less than zero but equal to zero) and some of the var1 and var2 are negative (which is less than zero)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;please try like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID$ Minutes Var1 Var2;
cards;
A 1 -3.2 -5.1
A 2 . -3.6
A 3 . -1.1
A 4 5.2 -4.9
B 1 3.2 1.2
B 2 4.5 1.2
B 3 3.1 4.2
C 1 . 1.9
C 2 . -2.2
C 3 2.1 -3.1
;
run;

data have1;
set have;
if var1 &amp;lt; 0 then Var1_minutes_below = var1;
if var2 &amp;lt;0 then Var2_minutes_below=var2;
run;

proc sql;
create table have2 as select id , max(Minutes) as minutes from have 
group by id;
quit;

proc sql;
create table have3 as select id , count(Var1_minutes_below) as Var1_minutes_below,
count(Var2_minutes_below) as Var2_minutes_below from have1
group by 1;
quit;

proc sql;
create table want as 
select a.*, a.Var1_minutes_below/b.minutes as Var1_perc_below , 
a.Var2_minutes_below/b.minutes as Var2_perc_below
from have3 as a left join have2 as b
on a.id = b.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jun 2018 15:54:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Percentage-of-time-for-a-numerical-variable-to-assume-a-value/m-p/471757#M120870</guid>
      <dc:creator>soham_sas</dc:creator>
      <dc:date>2018-06-20T15:54:11Z</dc:date>
    </item>
    <item>
      <title>Re: Percentage of time for a numerical variable to assume a value below a given threshold</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Percentage-of-time-for-a-numerical-variable-to-assume-a-value/m-p/472008#M120969</link>
      <description>&lt;P&gt;Thank you so much for your reply! This&amp;nbsp;solution works absolutely fine! As I'm not used to working with proc sql,&amp;nbsp;I think I will start with the solution posted above using only the data step, which I'm more familiar with.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Alexander&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 08:26:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Percentage-of-time-for-a-numerical-variable-to-assume-a-value/m-p/472008#M120969</guid>
      <dc:creator>AlexanderL</dc:creator>
      <dc:date>2018-06-21T08:26:03Z</dc:date>
    </item>
  </channel>
</rss>

