|
Also without the COUNT function (I did not do the renames, but you can do
that also. I think the disatvantage is the length of the big list which is
too much for the 5-char values...):
data a;
input id: $5. nbr_mc_l: $50. detail;
cards;
12345 mc134,mc345,mc324 1.25
67891 mc333 3.11
56547 mc324,mc456 3.25
;
run;
data expanded;
length nbr_mc $5;
set a;
nbr_mc=scan(nbr_mc_l,1,",");
i=1;
do while (nbr_mc ne " ");
output;
i+1;
nbr_mc=scan(nbr_mc_l,i,",");
end;
drop nbr_mc_l;
run;
On Wed, 22 Nov 2006 10:50:38 -0500, Jack Clark <JClark@CHPDM.UMBC.EDU> wrote:
>Jeri,
>
>The example below takes uses your sample data, and I believe does what you
>asked for. The count function is used to determine how many values of mc
>number exist in the NBR_MC field (by counting the number of commas, then
>adding 1 - this will need modification if any mc number can have a comma in
>it).
>
>Then, the number of mc numbers in NBR_MC is used as the upper bound in a DO
>loop. Each time through the loop, the SCAN function is used to select (left
>to right) one of the mc numbers from the NBR_MC field and output to a new
>record with the ID and DETAIL fields.
>
>data test;
>input id $ nbr_mc $20. detail;
>cards;
>12345 mc134,mc345,mc324 1.25
>67891 mc333 3.11
>56547 mc324,mc456 3.25
>;
>run;
>
>
>data test2 (keep=id detail nbr_mc_new rename=(nbr_mc_new=nbr_mc));
> set test;
>do i = 1 to count(nbr_mc,',')+1;
> nbr_mc_new = scan(nbr_mc,i,',');
> output;
>end;
>run;
>
>proc print data = test2;
>run;
>
>
>Jack Clark
>Research Analyst
>Center for Health Program Development and Management
>University of Maryland, Baltimore County
>
>
>
>-----Original Message-----
>From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Jeri Ji
>Sent: Wednesday, November 22, 2006 9:46 AM
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: Re: Basic do loop question
>
>Thank you very much, Kevin and Mark. Both of you answered my question. I
>will try the code that you guys suggested.
>
>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
>
>I am a new sas user and never used do loop before. I think maybe I can use
>a outer do loop loop from the 1st record to the end, and then an inner
>loop to break up the nbr_mc. The thing is first the number of rows in the
>dataset is not fixed. Second, the number of mc numbers in that column is
>not fixed either. I have the idea, but didn't figure out how to code it
>yet.
>
>Thank you very much.
>
>
>
>Jeri
>
>
>
> Kevin Roland Viel <kviel@EMORY.EDU>
> Sent by: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
> 11/21/2006 06:58 PM
> Please respond to
>Kevin Roland Viel <kviel@EMORY.EDU>
>
>
>To
>SAS-L@LISTSERV.UGA.EDU
>cc
>
>Subject
>Re: Basic do loop question
>
>
>
>
>
>
>On Tue, 21 Nov 2006, Jeri Ji wrote:
>
>> 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 ?
>
>Jeri,
>
> SAS provides this with a minimal amount of coding:
>
>data two ;
> set one ;
>run ;
>
> Of course, it is implicit. You want something more:
>
>data two ;
> do _n_ = 1 by 1 until ( end ) ;
> set one end = end ;
> end ;
>run ;
>
> This is a DOW-loop, or a Whitlock Do-Loop. Note that the default OUTPUT
>statement is no longer in effect.
>
> You could also use:
>
>data two ;
> do _n_ = 1 to nobs ;
> set one nobs = nobs ;
> end ;
> STOP ;
>run ;
>
> If you describe your goal and suggest a design, we could probably offer
>more tailored advice...
>
>HTH,
>
>Kevin
>
>PS You may also want to look at the POINT= option to the SET statement.
>
>Kevin Viel
>PhD Candidate
>Department of Epidemiology
>Rollins School of Public Health
>Emory University
>Atlanta, GA 30322
|