LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (November 2006, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Wed, 22 Nov 2006 16:11:04 +0000
Reply-To:     toby dunn <tobydunn@HOTMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         toby dunn <tobydunn@HOTMAIL.COM>
Subject:      Re: Basic do loop question
Comments: To: Mterjeson@RUSSELL.COM
In-Reply-To:  <16FD64291482A34F995D2AF14A5C932C015A6E1B@MAIL002.prod.ds.russell.com>
Content-Type: text/plain; format=flowed

Mark ,

I am going to tweak your code out using V9, hope ya dont mind:

Data Need ( Drop = I Old_Nbr_Mc ) ; Set Sample ( Rename = ( Nbr_Mc = Old_Nbr_Mc ) ) ;

Do I = 1 To ( CountC( Old_Nbr_Mc , ',' ) + 1 ) ; Nbr_Mc = Scan( Old_Nbr_Mc , I , ',' ) ; Output ; End ;

Run ;

Toby Dunn

Quickly, bring me a beaker of wine, so that I may wet my mind and say something clever. Aristophanes

Wise people, even though all laws were abolished, would still lead the same life. Aristophanes

You should not decide until you have heard what both have to say. Aristophanes

From: "Terjeson, Mark" <Mterjeson@RUSSELL.COM> Reply-To: "Terjeson, Mark" <Mterjeson@RUSSELL.COM> To: SAS-L@LISTSERV.UGA.EDU Subject: Re: Basic do loop question Date: Wed, 22 Nov 2006 08:03:14 -0800

Hi Jeri,

Providing a small sample of what you have, and what you want, is perfect for getting more concise and applicable answers. What you desire really does not need the number of observations, but the number of mc### elements within one variable only. The iterative nature of the DATA step will automatically iterate through all the incoming observations with the SET statement. All the lines of code between the DATA and RUN statements will execute on each obs. Without an OUTPUT statement there is one implied OUTPUT at the RUN statement. If we add an OUTPUT statement anywhere in our datastep then the automatic one at the RUN statement is disabled and you basically take manual control of the outputs whereever your one or more OUTPUT statements occur. With this in mind then all we have to do is output whatever is in the PDV (Program Data Vector) [the input/output variable buffer] for as many times as we find mc### in the nbr_mc variable. Therefore we do a couple things to accomplish that. First, we are going to need to use the nbr_mc variable for our output containing only one mc### element at a time, so we need to initially upon each observation save the whole string off to a temporary character variable(stmp) so that we keep all of the mc###'s. At this point we could count commas and just add 1 to the result to reach the number of mc### arguments. Or at the same time if we add one extra comma and not add 1 to get the number of args. Both achieve the same result but just in case you have an empty string we can avoid a little SAS nuance that you always get 1 from the LENGTH() function, which this scheme avoids. Then a mere DO loop from 1 to the number of commas and the SCAN function will grab string chunks inbetween whatever delimiter you specify. Effectively chopping out each mc### and placing each in the nbr_mc variable. An OUTPUT at that point gets you your result set as desired. The TRIM() function is used to truncate the trailing spaces of the nbr_mc variable before concatenating on the extra comma, this way the added comma is right against the end of the text. The DROP will throw away the temporary variables so that they do not end up in the outgoing dataset.

data sample; input id nbr_mc $24. detail; cards; 12345 mc134,mc345,mc324 1.25 67891 mc333 3.11 56547 mc324,mc456 3.25 ; run;

data result(drop=i comma_count stmp); set sample; stmp = trim(nbr_mc)||','; comma_count = length(stmp)-length(compress(stmp,',')); do i = 1 to comma_count; nbr_mc = scan(stmp,i,','); output; 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@LISTSERV.UGA.EDU] On Behalf Of Jeri Ji Sent: Wednesday, November 22, 2006 6: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

_________________________________________________________________ Talk now to your Hotmail contacts with Windows Live Messenger. http://clk.atdmt.com/MSN/go/msnnkwme0020000001msn/direct/01/?href=http://get.live.com/messenger/overview


Back to: Top of message | Previous page | Main SAS-L page