BookmarkSubscribeRSS Feed
jonatan_velarde
Lapis Lazuli | Level 10

Good morning my SAS friends:

 

Here is a question about SAS basic computing

 

As we know, confidence interval (CLASS) is a usual statistical procedure where we group many observations in CLASSES considering the upper and lower limits and the number of observations (n) as grouping criteria (see this example http://www.vitutor.com/statistics/descriptive/frequency_distribution.html)

 

Considering these data:

 

3, 15, 24, 28, 33, 35, 38, 42, 43, 38, 36, 34, 29, 25, 17, 7, 34, 36, 39, 44, 31, 26, 20, 11, 13, 22, 27, 47, 39, 37, 34, 32, 35, 28, 38, 41, 48, 15, 32, 13.

 

In this case, 48 − 3 = 45. For the purpose of this table, increase the number to 50. Therefore, 50 : 5 = 10 intervals.

 
         xi    fi    Fi    ni    Ni
[0, 5)        2.5    1    1    0.025    0.025
[5, 10)        7.5    1    2    0.025    0.050
[10, 15)    12.5    3    5    0.075    0.125
[15, 20)    17.5    3    8    0.075    0.200
[20, 25)    22.5    3    11    0.075    0.275
[25, 30)    27.5    6    17    0.150    0.425
[30, 35)    32.5    7    24    0.175    0.600
[35, 40)    37.5    10    34    0.250    0.850
[40, 45)    42.5    4    38    0.100    0.950
[45, 50)    47.5    2    40    0.050    1
                  40         1    

 

Is there any SAS Statement that could help???

 

thanks in advance

5 REPLIES 5
PaigeMiller
Diamond | Level 26

These are NOT confidence intervals.

 

You want to apply a format that groups your data into the intervals shown, and then run PROC FREQ on this data.

 

Something like this (where you do the rest of the typing on line 2)

proc format;
    value xf 0-4.99='0-5' 5-9.99='5-10' ... ;
run;

proc freq;
    table x;
    format xf.;
run;
--
Paige Miller
jonatan_velarde
Lapis Lazuli | Level 10
proc freq;
110 table x;
111 format xf.;
___
22
76
ERROR 22-322: Syntax error, expecting one of the following: a name, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.
ERROR 76-322: Syntax error, statement will be ignored.
Reeza
Super User

Where's the data = in proc freq? Check proc before as well for no errors. 

PaigeMiller
Diamond | Level 26

@jonatan_velarde wrote:
proc freq;
110 table x;
111 format xf.;
___
22
76
ERROR 22-322: Syntax error, expecting one of the following: a name, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.
ERROR 76-322: Syntax error, statement will be ignored.

Simple omission on my part. The command should be

 

format x xf.;

--
Paige Miller
rogerjdeangelis
Barite | Level 11
WPS/SAS/R Class (confidence) intervals

IML has an interface to R
IML might have this function, I am not sure.

I think you want left close ']' otherwise tou could miss and observation

https://communities.sas.com/t5/General-SAS-Programming/Class-confidence-intervals/m-p/347383
HAVE
====

Up to 40 obs SD1.HAVE total obs=50

Obs    NUMS

  1      3
  2     15
  3     24
  4     28
  5     33
  6     35
  7     38
  8     42
  9     43
...
 46     42
 47     43
 48     14
 49     30
 50     10

WANT
====

Up to 40 obs from c2wps total obs=10

Obs      C1       FREQ

  1    (0,5]        1
  2    (5,10]       2
  3    (10,15]      7
  4    (15,20]      2
  5    (20,25]      4
  6    (25,30]      6
  7    (30,35]     11
  8    (35,40]      8
  9    (40,45]      6
 10    (45,50]      3


The MEANS Procedure

                          Analysis Variable : NUMS

             N                                       Lower 95%       Upper 95%
C1         Obs            Mean         Std Dev     CL for Mean     CL for Mean
------------------------------------------------------------------------------
(0,5]        1       3.0000000               .               .               .
(5,10]       2       8.5000000       2.1213203     -10.5593071      27.5593071
(10,15]      7      13.5714286       1.3972763      12.2791636      14.8636936
(15,20]      2      18.5000000       2.1213203      -0.5593071      37.5593071
(20,25]      4      23.2500000       1.5000000      20.8631653      25.6368347
(25,30]      6      28.0000000       1.4142136      26.5158739      29.4841261
(30,35]     11      33.2727273       1.5550504      32.2280302      34.3174244
(35,40]      8      37.6250000       1.1877349      36.6320287      38.6179713
(40,45]      6      42.5000000       1.0488088      41.3993426      43.6006574
(45,50]      3      47.6666667       0.5773503      46.2324491      49.1008842
------------------------------------------------------------------------------


WORKING CODE

  R
     c1 <- cut(have$NUMS, breaks = seq(0, 50, by = 5));
     proc means

*                _                  _       _
 _ __ ___   __ _| | _____        __| | __ _| |_ __ _
| '_ ` _ \ / _` | |/ / _ \_____ / _` |/ _` | __/ _` |
| | | | | | (_| |   <  __/_____| (_| | (_| | || (_| |
|_| |_| |_|\__,_|_|\_\___|      \__,_|\__,_|\__\__,_|

;

options validvarname=upcase;
libname sd1 "d:/sd1";
data sd1.have;
input nums @@;
cards4;
3 15 24 28 33 35 38 42 43 38 36 34 29 25 17 7 34 36 39 44 31
26 20 11 13 22 27 47 39 37 34 32 35 28 38 41 48 15 32 13
14 22 35 48 31 42 43 14 30 10
;;;;
run;quit;

*____                 _       _   _
|  _ \      ___  ___ | |_   _| |_(_) ___  _ __
| |_) |____/ __|/ _ \| | | | | __| |/ _ \| '_ \
|  _ <_____\__ \ (_) | | |_| | |_| | (_) | | | |
|_| \_\    |___/\___/|_|\__,_|\__|_|\___/|_| |_|

;

%utl_submit_wps64('
libname sd1 "d:/sd1";
options set=R_HOME "C:/Program Files/R/R-3.3.2";
libname wrk "%sysfunc(pathname(work))";
proc r;
submit;
library(haven);
have<-read_sas("d:/sd1/have.sas7bdat");
c1 <- cut(have$NUMS, breaks = seq(0, 50, by = 5));
c1;
c2<-as.data.frame(table(c1));
endsubmit;
import r=c2 data=wrk.c2wps;
import r=c1 data=wrk.c1wps;
run;quit;
');

data cutval;
  merge c1wps sd1.have;
run;quit;

/*
Up to 40 obs WORK.CUTVAL total obs=50

Obs      C1       NUMS

  1    (0,5]        3
  2    (10,15]     15
  3    (20,25]     24
  4    (25,30]     28
  5    (30,35]     33
  6    (30,35]     35
  7    (35,40]     38
  8    (40,45]     42
*/


proc means data=cutval mean std clm;
class c1;
var nums;
run;quit;


NOTE: Using R version 3.3.2 (2016-10-31) from C:/Program Files/R/R-3.3.2

NOTE: Submitting statements to R:

> library(haven);
> have<-read_sas("d:/sd1/have.sas7bdat");
> c1 <- cut(have$NUMS, breaks = seq(0, 50, by = 5));
> c1;
> c2<-as.data.frame(table(c1));

NOTE: Processing of R statements complete

12        import r=c2 data=wrk.c2wps;
NOTE: Creating data set 'WRK.c2wps' from R data frame 'c2'
NOTE: Column names modified during import of 'c2'
NOTE: Data set "WRK.c2wps" has 10 observation(s) and 2 variable(s)

13        import r=c1 data=wrk.c1wps;
NOTE: Creating data set 'WRK.c1wps' from R data frame 'c1'

NOTE: Column names modified during import of 'c1'

NOTE: Data set "WRK.c1wps" has 50 observation(s) and 1 variable(s)

14        run;
NOTE: Procedure r step took :
      real time : 0.655
      cpu time  : 0.015


15        quit;

NOTE: Submitted statements took :
      real time : 0.702
      cpu time  : 0.046

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