home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / bit / listserv / sasl / 5000 < prev    next >
Encoding:
Text File  |  1992-11-10  |  6.9 KB  |  225 lines

  1. Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU
  2. Path: sparky!uunet!stanford.edu!bcm!convex!darwin.sura.net!paladin.american.edu!auvm!DMRHRZ11.BITNET!SCHICK
  3. Message-ID: <SAS-L%92111014214220@AWIIMC12.IMC.UNIVIE.AC.AT>
  4. Newsgroups: bit.listserv.sas-l
  5. Date:         Tue, 10 Nov 1992 14:01:01 CET
  6. Reply-To:     Arnold Schick <SCHICK@DMRHRZ11.BITNET>
  7. Sender:       "SAS(r) Discussion" <SAS-L@UGA.BITNET>
  8. From:         Arnold Schick <SCHICK@DMRHRZ11.BITNET>
  9. Subject:      Re: Thanks, new group id values from the old one
  10. Lines: 213
  11.  
  12. Hallo SAS-Lers,
  13.  
  14. last week, I searched a solution for new group id values from an old,
  15. previously created data set. Many suggestions came from members of
  16. the list, and solves the problem.
  17.  
  18. To complete this Thanks list here, I would like to express my
  19. sincere and grateful thanks also to Joanne Benson, Mark D. H. Miller,
  20. Linda Neden, and Matthew Zack, who sent me also solutions.
  21.  
  22. The question was:
  23.  
  24. from an old data set should have a new data set the id values with its
  25. sequence ... , as in the following example:
  26.  
  27.         data old          data calc                data new
  28.         X    Y     ID     X    Y     ID            X    Y     ID
  29.         1    1     A      1    1     B             1    1     A
  30.         2    2     A      1.2  1.7   B             1.2  1.7   A
  31.         3    3     A      2    2     B             2    2     A
  32.         2    5     B      3.1  3.6   B             3.1  3.6   A
  33.         1    6     B      3    3     B             3    3     A
  34.         0    4     B      1.9  4.9   C             1.9  4.9   B
  35.         1.5  3     B      2    5     C             2    5     B
  36.         6    2     D      2.5  5.1   C             2.5  5.1   B
  37.         7    3     D      1    6     C             1    6     B
  38.                           0.3  5.5   C             0.3  5.5   B
  39.                           0    4     C             0    4     B
  40.                           6    2     E             6    2     D
  41.                           6.2  2.7   E             6.2  2.7   D
  42.                           7    3     E             7    3     D
  43.  
  44. Here, I'll present two different solutions (1th, is for sorted data set
  45. ONE and 2nd, for sorted/notsorted data set ONE) and hope that's possible
  46. helpful for others - both running with SAS 6.04 and 6.07 -,
  47.  
  48. Arnold Schick  University of Marburg
  49. -------------------------------------------------------------------------
  50.    *                                                             ;
  51.    * read in data from data set, old.;
  52.    *  create a new variable for each identification key, and write;
  53.    *   this variable to a new data set.                          ;
  54.    *                                                             ;
  55.    DATA OLD(KEEP=X Y ID)
  56.         OLDID(KEEP=OLDID);
  57.      RETAIN OLDID;
  58.      INFILE CARDS;
  59.      INPUT X Y ID $;
  60.      IF _N_ EQ 1 THEN DO;
  61.         OLDID=ID;
  62.         OUTPUT OLDID;
  63.      END;
  64.      ELSE DO;
  65.         IF ID NE OLDID THEN DO;
  66.            OLDID=ID;
  67.            OUTPUT OLDID;
  68.         END;
  69.      END;
  70.      OUTPUT OLD;
  71.    CARDS;
  72.           1    1     A
  73.           2    2     A
  74.           3    3     A
  75.           2    5     B
  76.           1    6     B
  77.           0    4     B
  78.           1.5  3     B
  79.           6    2     D
  80.           7    3     D
  81.  
  82.      ;
  83.    *                                                             ;
  84.    * create new data set, calc.;
  85.    *  here, read in new values because transformations of data set,;
  86.    *  old, to data set, calc, not specified in original query.   ;
  87.    *  create a new variable for each identification key, and write;
  88.    *   this variable to a new data set.                          ;
  89.    *                                                             ;
  90.    DATA CALC(KEEP=X Y ID)
  91.         CALCID(KEEP=CALCID);
  92.      RETAIN CALCID;
  93.      INFILE CARDS;
  94.      INPUT X Y ID $;
  95.      IF _N_ EQ 1 THEN DO;
  96.         CALCID=ID;
  97.         OUTPUT CALCID;
  98.      END;
  99.      ELSE DO;
  100.         IF ID NE CALCID THEN DO;
  101.            CALCID=ID;
  102.            OUTPUT CALCID;
  103.         END;
  104.      END;
  105.      OUTPUT CALC;
  106.    CARDS;
  107.         1    1     B
  108.         1.2  1.7   B
  109.         2    2     B
  110.         3.1  3.6   B
  111.         3    3     B
  112.         5   11     A
  113.         6   10     A
  114.         1.9  4.9   C
  115.         2    5     C
  116.         2.5  5.1   C
  117.         1    6     C
  118.         6    2     D
  119.         2    9     D
  120.         5    1.7   D
  121.         4    2.5   D
  122.         7    3     D
  123.    ;
  124.    *                                                             ;
  125.    * perform side-by-side merge of the identification keys from  ;
  126.    *  the data sets, old and calc;
  127.    *                                                             ;
  128.    DATA BOTHID(KEEP=ID OLDID);
  129.      MERGE CALCID(RENAME=(CALCID=ID)) OLDID;
  130.      OUTPUT BOTHID;
  131.    RUN;
  132.    RUN;
  133.    *                                                             ;
  134.    * sort the data set, calc,;
  135.    *  by the identification key.;
  136.    *                                                             ;
  137.    PROC SORT DATA=CALC;
  138.      BY ID;
  139.    RUN;
  140.    *                                                             ;
  141.    * sort the data set, bothid,;
  142.    *  by the identification key from the data set, calc.;
  143.    *                                                             ;
  144.    PROC SORT DATA=BOTHID;
  145.      BY ID;
  146.    RUN;
  147.    *                                                             ;
  148.    * merge the data set, bothid, and the data set, calc,;
  149.    *  by their common identification key.                        ;
  150.    *                                                             ;
  151.    DATA NEW(KEEP=X Y OLDID RENAME=(OLDID=ID));
  152.      MERGE CALC BOTHID;
  153.      BY ID;
  154.      OUTPUT NEW;
  155.    RUN;
  156.    *                                                             ;
  157.    * print data set, new.;
  158.    *                                                             ;
  159.    PROC PRINT DATA=NEW;
  160.      VAR X Y ID;
  161.      TITLE1 "DATA SET, NEW";
  162.    RUN;
  163. *------------------- end of program 1 ---------------------------;
  164. The 2nd SAS program solves this for a NOTSORTED data set ONE:
  165.  
  166.  data old;
  167.    input x y id $ ;
  168.    cards;
  169.           1    1     A
  170.           2    2     A
  171.           3    3     A
  172.           5   11     K
  173.           6   10     K
  174.           2    5     B
  175.           1    6     B
  176.           0    4     B
  177.           1.5  3     B
  178.           6    2     D
  179.           7    3     D
  180. ;
  181. data calc;
  182.   input x y id $ ;
  183.   cards;
  184.         1    1     B
  185.         1.2  1.7   B
  186.         2    2     B
  187.         3.1  3.6   B
  188.         3    3     B
  189.         5   11     A
  190.         6   10     A
  191.         1.9  4.9   C
  192.         2    5     C
  193.         2.5  5.1   C
  194.         1    6     C
  195.         6    2     D
  196.         2    9     D
  197.         5    1.7   D
  198.         4    2.5   D
  199.         7    3     D
  200. ;
  201. data one;
  202.   set old (rename=(id=old_id));
  203.   by old_id notsorted;
  204.   if first.old_id;
  205.   keep old_id;
  206. run;
  207. data two;
  208.   set calc;
  209.   by id notsorted;
  210.   if first.id;
  211.   keep id;
  212. run;
  213. data id_match;
  214.   merge one two;
  215. run;
  216. data new;
  217.   merge calc id_match; by id notsorted;
  218.   id=old_id;
  219.   drop old_id;
  220. run;
  221. title 'data ID-Match';
  222. proc print data=id_match; run;
  223. title 'data NEW with old ID-order';
  224. proc print data=new; run;
  225.