|
|||
|
Summary: 1) Simplify
2) Reduce wide structure to long and back with a loop #iw-value=1 Gehard, Simplify, no need for two buffers. data x ( drop = i ) ; do i=1 to no; set dataset point=i nobs = no ; output; end; stop; /* very important */ run; Or even better. data x; do until ( eof ) ; set dataset end = eof ; output; end; run; While I believe it is a very important step in one's SAS programming education to understand looping and learn the rhythms available when either the input or output is in an explicit loop, I have to ask why any of the constructs shown above or below are better than data x ; set dataset ; run ; Unfortunately the answer can only lie in the details of the problem. As shown all of the above accomplish no more than making a copy of DATASET called X. Hence they are worthless without more code that will be dependent on some problem. Jeri, If you say what you want to accomplish rather than how, you will probably get better advice. Ah, in looking for the address I found What I need to do is that I have a dataset look like the following: id nbr_mc detail 12345 mc134,mc345,mc324 1.25 67891 mc333 3.11 56547 mc324,mc456 3.25 . . . . . . And I need the final result to look like this: id nbr_mc detail 12345 mc134 1.25 12345 mc345 1.25 12345 mc324 1.25 67891 mc333 3.11 56547 mc324 3.25 56547 mc456 3.25 Consider the following. data w ; input id nbr_mc :$50. detail ; cards ; 12345 mc134,mc345,mc324 1.25 67891 mc333 3.11 56547 mc324,mc456 3.25 ; data wanted ( drop = _nbr_mc ) ; length nbr_mc $ 5 ; set w ( rename = ( nbr_mc=_nbr_mc) ) ; do seq = 1 by 1 ; nbr_mc = scan(_nbr_mc,seq) ; if seq > 1 and nbr_mc = " " then leave ; output ; end ; run ; Note that the SET statement is not in the loop and the OUTPUT is in the loop because we want to process one record and make many records output. To reverse the above process you would put the SET in the loop. data w2 ( drop = seq _nbr_mc ) ; length nbr_mc $ 50 ; do until ( last.id ) ; set wanted ( rename = (nbr_mc=_nbr_mc) ) ; by id notsorted ; nbr_mc = cats(nbr_mc,",",_nbr_mc) ; /* requires v9 */ end ; nbr_mc = substr(nbr_mc,2) ; run ; If version 9 is not available then replace the offending line with nbr_mc = trimn(nbr_mc) || "," || _nbr_mc ; Ian Whitlock ============== Date: Tue, 21 Nov 2006 20:48:28 -0500 Reply-To: Gerhard Hellriegel <gerhard.hellriegel@T-ONLINE.DE> Sender: "SAS(r) Discussion" From: Gerhard Hellriegel <gerhard.hellriegel@T-ONLINE.DE> Subject: Re: Basic do loop question There are some other things which must be cleared: - why do you want that? This loop is automatic (default). A SET in a DATA step starts with the first obs and ends after the last. - if you want to do it anyway - how do you get the next obs after working with the first? There is no GET or READ in SAS for that. You could use the POINT= dataset option for that, if you want to do it. But be careful: data x; set dataset nobs=no; /* to get the number of obs */ do i=1 to no; set dataset point=i; output; end; stop; /* very important */ run; if you forget the STOP, you might have a endless loop! Reason: with the first SET, you start the automatic loop. In that loop you have the other loop which starts with i=1 and goes through the dataset. That loop ends and you have positioned the loop-pointer to a valid obs-number. The automatic loop iterates again and execute the inner loop - again and again... So it's a good idea to stop execution after the first completion of the inner loop. The code above is the same like data x; set dataset; run; Regards, Gerhard On Tue, 21 Nov 2006 15:35:50 -0800, Terjeson, Mark <Mterjeson@RUSSELL.COM> wrote: >Hi Jeri, > >You can use PROC SQL and &SQLOBS after, >or you can use datastep and nobs= on the >SET statement. But these will generate >and ERROR if the dataset does not exist. > >This method below does not get an ERROR >if the dataset should not exist. > >Of course, change the SASHELP and CLASS >to your lib token and dataset name. > > > >%let NUMOBS=0; * set default ; >proc sql noprint; > select nobs into :NUMOBS > from sashelp.vtable > where upcase(libname) eq 'SASHELP' > and upcase(memname) eq 'CLASS'; >quit; > > >data _null_; > do i = 1 to &NUMOBS; > put i=; > end; >run; > > > >Hope this is helpful. > > >Mark Terjeson >Senior Programmer Analyst, IM&R >Russell Investment Group > > >Russell >Global Leaders in Multi-Manager Investing > > > > > > >-----Original Message----- >From: SAS(r) Discussion [mailto:SAS-L] On Behalf Of >Jeri Ji >Sent: Tuesday, November 21, 2006 2:56 PM >To: SAS-L >Subject: Basic do loop question > >I am going to use do loop the first time in sas and have a basic >question. >what should I do if I want the do loop start from the 1st row in the >dataset and exit after the last row (the number of rows are not fixed >for each run)? > >do i=1 to ? > >Thanks. > > >Jeri |
|
|
||||
|
||||
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: Basic Do Loop Question... | nospam@HOWLES.COM (Howard Schreier | Newsgroup comp.soft-sys.sas | 0 | 05-26-2007 02:40 AM |
| Re: Basic do loop question | toby dunn | Newsgroup comp.soft-sys.sas | 0 | 11-22-2006 03:11 PM |
| Re: Basic do loop question | Jack Clark | Newsgroup comp.soft-sys.sas | 0 | 11-22-2006 02:50 PM |
| Re: Basic do loop question | Jeri Ji | Newsgroup comp.soft-sys.sas | 0 | 11-22-2006 01:46 PM |
| Re: Basic do loop question | Gerhard Hellriegel | Newsgroup comp.soft-sys.sas | 0 | 11-22-2006 12:48 AM |