home *** CD-ROM | disk | FTP | other *** search
- Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU
- Path: sparky!uunet!uvaarpa!darwin.sura.net!bogus.sura.net!howland.reston.ans.net!paladin.american.edu!auvm!UNC.BITNET!UPHILG
- Return-Path: <@OHSTVMA.ACS.OHIO-STATE.EDU:SAS-L@VTVM2.BITNET>
- Message-ID: <SAS-L%93012612402902@VTVM2.CC.VT.EDU>
- Newsgroups: bit.listserv.sas-l
- Date: Tue, 26 Jan 1993 12:36:00 EST
- Reply-To: "Philip Gallagher,(919)966-1065" <UPHILG@UNC.BITNET>
- Sender: "SAS(r) Discussion" <SAS-L@UGA.BITNET>
- From: "Philip Gallagher,(919)966-1065" <UPHILG@UNC.BITNET>
- Subject: Changing order of variables in a SAS dataset
- Comments: cc: ted@SERVER.SASW.NCSU.EDU
- Lines: 52
-
- Ted Greenstein <TED@SERVER.SASW.NCSU.EDU>
- writes enquiring about changing the order of variables
- in a SAS dataset. I knew that I didn't know the answer to
- this, for I have never had the need to do it. I suppose it
- might be useful if one were preparing datasets for students
- who were too naive to use VAR stmnts, or something like that,
- though. Ted wrote asking for a solution that works for 6.08
- in Windows; my soln was tested in 5.18, but I hope it will work
- for 6.08 as well.
-
- My first expectation was that clever use of either a KEEP stmnt
- or a KEEP= dataset option would accomplish the re-ordering, but
- I tried it and proved to myself that neither device reordered the
- variables in any of the ways that I implemented them.
-
- Then I resorted to (first) something I knew would work, namely
- creating a dummy (no observations) dataset using a datastep with
- nothing but a LENGTH stmnt in which I ordered the variables in
- the desired order. Then, I created a new dataset by concatenating
- the old (unwanted order) dataset onto the dummy (desired order)
- dataset, for I knew that SAS would take the order from the first
- dataset it encountered. This worked. (I was using 5.18, and
- the resulting concatenation had an unexpect first observation
- of all missing values from the nominally 0 observation dummy
- dataset, and I had to put in a stmnt to delete that observation,
- - unnecessarily nasty complication - I think it is different in
- 6.04, but I haven't checked yet.)
-
- Finally I tried using the LENGTH stmnt in a datastep, preceding
- the SET stmnt; as I expected, everything worked fine.
- Here is the code that produced the final result; needless to
- say, it is much simpler than producing a dummy dataset and
- concatenating onto it.
-
- I am surprised that the KEEP stmnt/dataset didn't do the
- reordering, but that shows how little I know about how
- the SAS compiler works when it is establishing the characteristics
- of a dataset. I know a tiny bit more, now.
-
- Phil Gallagher
- uphilg@unc
-
- DATA NEW;
- ARRAY ZAP {10} var1-var10;
- DO I = 1 TO 10; ZAP{I} = I; END; drop i;
- RUN;
- PROC PRINT; TITLE 'Original order'; run;
-
- DATA NEWEST;
- length VAR10 VAR9 VAR8 VAR7 VAR6 VAR5 VAR4 VAR3 VAR2 VAR1 8;
- SET NEW; RUN;
- PROC PRINT; TITLE 'Order via DATAstep LENGTH stmnt'; run;
-