home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / bit / listserv / sasl / 3910 < prev    next >
Encoding:
Text File  |  1992-08-26  |  7.3 KB  |  256 lines

  1. Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU
  2. Path: sparky!uunet!paladin.american.edu!auvm!GTE.COM!GLG2
  3. X-Mailer: ELM [version 2.3 PL10]
  4. Message-ID: <9208261741.AA17513@bunny.gte.com>
  5. Newsgroups: bit.listserv.sas-l
  6. Date:         Wed, 26 Aug 1992 13:41:33 EDT
  7. Reply-To:     Gail Gill <glg2@GTE.COM>
  8. Sender:       "SAS(r) Discussion" <SAS-L@UGA.BITNET>
  9. From:         Gail Gill <glg2@GTE.COM>
  10. Subject:      SQL responses
  11. Comments: To: sas-l%marist.bitnet@cunyvm.cuny.edu
  12. Lines: 242
  13.  
  14. I'd like to add to that list of thanks Richard Wright, and Joseph
  15. Schunk.
  16.  
  17. >Would you be kind enough to send me a copy of their responses, or else
  18. >post a summary to the list? Thanks much in advance.
  19.  
  20. >- anup k. roy, u. of illinois at urbana-champaign
  21.  
  22. No problem, I kept a file and have done some editing of the responses.
  23. I wound up trying a version of what Kernon sent, but since I was also
  24. trying to do something else with it, my convoluted version did not
  25. work. What I wound up doing was as Richard Wright suggested, but
  26. again, a convoluted version, since I needed to do some other things
  27. with it as well. I hope this helps you out.
  28.  
  29. Gail Gill
  30. glg2@gte.com
  31.  
  32.  
  33.  
  34. From Bob Upson:
  35.     (this specifically ignores your req for sql- if you've a reason for
  36. using sql, feel free to discard.)
  37.     sort the data by id,transtype, cost,
  38. retain a counter, when the cost within type within id changes create the
  39. composite var, output, and reset the counter.
  40. code frag:
  41.    ...; by id tipe cst; cnt+1; if last.cst then do;
  42. compos=put(cnt,?.) :: '*' :: put(cst,?.); output;cnt=0;end;
  43. (drop cst cnt;)
  44.  
  45. From Bob Snyder:
  46. E-ADDR:    rsnyder@lobby.ti.com
  47. NAME:      Bob Snyder
  48. COMPANY:   Texas Instruments, Sherman, TX
  49. PHONE:     (903) 868-5799
  50. FAX:       (903) 868-7240
  51.  
  52. Try the following:
  53.  
  54. data sasuser.gailgill;
  55.  
  56.   length Cust 8 item $ 1 cost 8;
  57.  
  58.   format cost dollar7.2;
  59.  
  60.   input Cust item cost;
  61.  
  62. cards;
  63.  1      A      2.00
  64.  1      A      2.00
  65.  1      A      2.00
  66.  1      B      6.00
  67.  1      B      6.00
  68.  2      A      2.00
  69.  2      C      4.00
  70.  2      D      5.00
  71.  2      E      7.00
  72. ;
  73.  
  74. run;
  75.  
  76. proc sql;
  77.   select cust, item, put( sum(cost), dollar7.2 ) as cost
  78.     from sasuser.gailgill
  79.       group by cust,item;
  80.  
  81. ============================== OUTPUT ===========================
  82.  
  83.     CUST  ITEM  COST
  84.    -----------------------
  85.      1  A       $6.00
  86.      1  B      $12.00
  87.      2  A       $2.00
  88.      2  C       $4.00
  89.      2  D       $5.00
  90.      2  E       $7.00
  91.  
  92.  
  93.  
  94.     This isn't an SQL solution, but assuming COST is a numeric variable, I'd
  95. do something like this ;
  96.  
  97. data temp;
  98. input @2 cust 1. @9 item $1. @16 cost 4.2 ;
  99. * Cust   item   cost ;
  100. cards;
  101.  1      A      1.23
  102.  1      A      1.23
  103.  1      A      1.23
  104.  1      B      8.77
  105.  1      B      8.77
  106.  2      A      1.23
  107.  2      C      7.55
  108.  2      D      6.81
  109.  2      E      6.33
  110. ;
  111. run;
  112. proc sort data=temp(rename=(cost=unitcost)) ;
  113.   by cust ;
  114. proc freq data=temp;
  115.   by cust ;
  116.   tables item /list out=results(rename=(count=cost) drop=percent) ;
  117.   weight unitcost ;
  118.   run;
  119.  
  120. ---------------------------------------------------------------------------
  121. Name         Steve James                          The new computer's a piece
  122. Mail Address SPJ1@CEHIEC1.EM.CDC.GOV              of junk, I'd really wish
  123. Real address Centers for Disease Control          they'd sell it.  It never
  124.              1600 Clifton Road NE, Mailstop F36   does a thing I want, only
  125.              Atlanta, GA  30333                   what I tell it.
  126.              (404) 488-4656                                  Author Unknown
  127.  
  128. From: Melvin Klassen <KLASSEN@UVVM.UVic.CA>
  129.  
  130. SELECT     CUST ITEM SUM(COST)
  131.  ORDER BY  CUST ITEM
  132.  
  133.  
  134.  
  135. I've included a SQL program to handle your request below.  Note that if
  136. you just wanted a report, you could leave off the CREATE TABLE... part.
  137. Also, this program works even if the cost isn't the same for each ITEM.
  138. This could also be done easily enough using PROC SUMMARY.
  139.  
  140. Kernon Gibes
  141. Internet: gibes@swirl.monsanto.com
  142.  
  143. ___ SAS program ________________________________________________________
  144.  
  145. data long;
  146.  input Cust   item $  cost;
  147. cards;
  148.  1      A      10
  149.  1      A      10
  150.  1      A      10
  151.  1      B      15
  152.  1      B      15
  153.  2      A      10
  154.  2      C      4
  155.  2      D      5
  156.  2      E      6
  157. ;
  158. run;
  159.  
  160. proc sql;
  161.   create table short as
  162.   select cust, item, sum(cost) as cost
  163.   from long
  164.   group by cust, item
  165.   ;
  166. quit;
  167.  
  168. proc print data=short;
  169. run;
  170.  
  171. ___ SAS output _________________________________________________________
  172.  
  173.                       OBS    CUST    ITEM    COST
  174.  
  175.                        1       1      A       30
  176.                        2       1      B       30
  177.                        3       2      A       10
  178.                        4       2      C        4
  179.                        5       2      D        5
  180.                        6       2      E        6
  181.  
  182.  
  183. Assuming you aren't trying to get symbolic results (and I really wish
  184. SAS would develope a symbolic math processor), this sounds like the prefect
  185. candidate for PROC TABULATE or SUMMARY / MEANS.
  186.  
  187. PROC TABULATE DATA=yourdata ;
  188.   CLASS cust item ;
  189.   VAR cost ;
  190.   TABLE cust*item,cost*SUM ;
  191.  
  192. will give you a nice table looking very much like your example.
  193.  
  194. You could create an output data set for further processing using either of
  195. the latter two procs:
  196.  
  197. PROC SUMMARY DATA=yourdata NWAY ;
  198.   CLASS cust item ;
  199.   VAR cost ;
  200.   OUTPUT OUT=outdata SUM= ;
  201.  
  202. will create an aggregated data set of costs by cust and item. As an added
  203. special advantaged, the data set will be sorted by cust and item, even if
  204. the input data set is not sorted. BTW, note the NWAY option - this yields
  205. unique cross-products of the two CLASS variables. If you don't use it, you
  206. will get grand totals and totals of cost for cust and item.
  207.  
  208. Needless to say, there are a multitude of options you can fiddle with to
  209. that just right look.
  210.  
  211.        ============================================
  212.       =  RRRRR ------ CCCCC ----- WW          WW =
  213.      = RR     RR  CC        CC - WW          WW =
  214.     = RR    RR - CC           - WW          WW =
  215.    = RRRRRRR -- CC           - WW          WW = Richard Wright
  216.   = RR    RR - CC           - WW    WW    WW = Tx. Leg. Bud. Board
  217.  = RR     RR -- CC    CC --- WW  WW  WW  WW = Internet:rcw@Tenet.edu
  218. = RR     RR ---- CCCCC ------ WWW --- WWW  = Compuserve: 70673,1131
  219. ===========================================
  220.  
  221.  
  222. proc sql;
  223.  
  224.   select
  225.     cust,
  226.     item,
  227.     cost as unitcost,
  228.     count(*) as quantity,
  229.     (unitcost * quantity) as extended
  230.     from
  231.       mytable
  232.    group  by
  233.      cust, item
  234. ;
  235.  
  236.   +------------------------------------------------------------+
  237.   |  MCI Mail:  JSchunk or 100-7026                            |
  238.   |  Telex (MCI/WUI): 6501007026   Ans: 6501007026MCI UW       |
  239.   |  Internet:  JSchunk@MCIMail.Com                            |
  240.   |  X.400: G=joseph; S=schunk; ADMD=mci; C=us;                |
  241.   +------------------------------------------------------------+
  242.  
  243. Wouldn't this be the answer:
  244.  
  245. select cust , item , sum(cost) as cost
  246. from yourdata
  247. group by cust , item
  248. order by cust , item
  249. ;
  250. _____________________________________________________________________________
  251.  Frank Poppe             <poppe@swov.nl>                tel:   +31 70 3209323
  252.  Institute for Road Safety Research SWOV                fax:   +31 70 3201261
  253.     ,,,  ,, ,, ,,  .... ,, ,,                           mail:      PO Box 170
  254.    ||,,, || || || :: :: || ||                            2260 AD Leidschendam
  255.    ,,,|~ ||,'|,~  :: :' ||,~                                  the Netherlands
  256.