DO LOOP Problem

DO LOOP Problem

Post by Elmaache, Hama » Sat, 16 Mar 2002 03:57:58



    Hi there.
I have the data:
 Job_code         Annual_Rate;

0010                18000.00
0010                10000.00
0010                20000.00
0010                15000.00
0011                  8000.00
0011                12000.00
0012                15000.00

I would have max and min for each Job_code, like this:

                                  max_     min_
                                             Annual_    Rate_    Rate_
                          Obs    Job_code      Rate      code     code

                           1        10        10000     20000    10000
                           2        11        12000     12000     8000
                           3        12        15000     15000    15000

I wrote the following code, but It doesn't work. What's wrong?
/**************************/
        data xx1;
        input Job_code    Annual_Rate;
        datalines;
0010                18000.00
0010                10000.00
0010                20000.00
0010                15000.00
0011                  8000.00
0011                12000.00
0012                15000.00
;
run;

        proc sort data=xx1 out=xx2;
        by Job_code ;
        run;
        /**************************/
        data xx3 ;
        retain Job_code  max_Rate_code n;
    set xx2  ;
        by Job_code ;
           max_Rate_code=0;
 do n = 1  until (last.Job_code) ;
        if Annual_Rate> max_Rate_code
        then  do;
           max_Rate_code=Annual_Rate;
           end;
        output;
 end;
        run;

        proc print data=xx3;
        run;
/**********************************/
Thanks a lot.
Barrere.

 
 
 

DO LOOP Problem

Post by Elmaache, Hama » Sat, 16 Mar 2002 04:22:23


Thanks.
I know how to do it with PROC SQL:
/**********************/
        data xx1;
        input Job_code    Annual_Rate;
        datalines;
0010                10000.00
0010                15000.00
0010                18000.00
0010                20000.00
0011                  8000.00
0011                12000.00
0012                15000.00
;
run;
        proc sql;
        create table xx2 as
        select *,
        max(Annual_Rate) as max_Rate_code,
        min(Annual_Rate) as min_Rate_code
        from xx1
        group by Job_code ;
        quit;

        proc print data=xx2;
        run;
        proc sort data=xx2 out=xx2;
        by Job_code ;

        data xx3; set xx2;
        by Job_code ;
        if first.Job_code=1;
        run;

        proc print data=xx3;
        run;
/**************************/
but I wante that's wrong in my code.
 Thanks a lot.

-----Original Message-----

Sent: March 14, 2002 2:14 PM

Subject: Re: DO LOOP Problem

TO:  Hamani Elmaache

Rather than the final data step and print, you could use either PROC
MEANS/SUMMARY or PROC SQL; I've included the PROC SQL to do what I think you
want to achieve.

PROC SQL;
     select distinct job_code, min(annual_rate) as min_annual_rate
label="Min Annual Rate",
                               max(annual_rate) as max_annual_rate
label="Max Annual Rate"
       from work.xx1
      group by job_code;

If you want  to list each Annual_Rate as well, then make the above a VIEW
(create view work.myview as), and merge the work.xx1 with this view by
Job_Code.

HTH

====================

Nicholson Warman
Data Analysis Consultant
Instructional, Research, and Client Services
Bryan Building Room 235A
University of North Carolina at Greensboro
(336) 334-5350
(336) 334-4367 [fax]


    Hi there.
I have the data:
 Job_code         Annual_Rate;

0010                18000.00
0010                10000.00
0010                20000.00
0010                15000.00
0011                  8000.00
0011                12000.00
0012                15000.00

I would have max and min for each Job_code, like this:

                                  max_     min_
                                             Annual_    Rate_    Rate_
                          Obs    Job_code      Rate      code     code

                           1        10        10000     20000    10000
                           2        11        12000     12000     8000
                           3        12        15000     15000    15000

I wrote the following code, but It doesn't work. What's wrong?
/**************************/
        data xx1;
        input Job_code    Annual_Rate;
        datalines;
0010                18000.00
0010                10000.00
0010                20000.00
0010                15000.00
0011                  8000.00
0011                12000.00
0012                15000.00
;
run;

        proc sort data=xx1 out=xx2;
        by Job_code ;
        run;
        /**************************/
        data xx3 ;
        retain Job_code  max_Rate_code n;
    set xx2  ;
        by Job_code ;
           max_Rate_code=0;
 do n = 1  until (last.Job_code) ;
        if Annual_Rate> max_Rate_code
        then  do;
           max_Rate_code=Annual_Rate;
           end;
        output;
 end;
        run;

        proc print data=xx3;
        run;
/**********************************/
Thanks a lot.
Barrere.


 
 
 

DO LOOP Problem

Post by Matthew Ne » Sat, 16 Mar 2002 04:33:57


A quick scan of the code shows no mention of the variable min_rate_code.

Matt

                    "Elmaache, Hamani"

                    DRC.GC.CA>                    cc:
                    Sent by: "SAS(r)              Subject:     DO LOOP Problem
                    Discussion"

                    >

                    14/03/02 10:57 AM
                    Please respond to
                    "Elmaache, Hamani"

    Hi there.
I have the data:
 Job_code         Annual_Rate;

0010                18000.00
0010                10000.00
0010                20000.00
0010                15000.00
0011                  8000.00
0011                12000.00
0012                15000.00

I would have max and min for each Job_code, like this:

                                  max_     min_
                                             Annual_    Rate_    Rate_
                          Obs    Job_code      Rate      code     code

                           1        10        10000     20000    10000
                           2        11        12000     12000     8000
                           3        12        15000     15000    15000

I wrote the following code, but It doesn't work. What's wrong?
/**************************/
        data xx1;
        input Job_code    Annual_Rate;
        datalines;
0010                18000.00
0010                10000.00
0010                20000.00
0010                15000.00
0011                  8000.00
0011                12000.00
0012                15000.00
;
run;

        proc sort data=xx1 out=xx2;
        by Job_code ;
        run;
        /**************************/
        data xx3 ;
        retain Job_code  max_Rate_code n;
    set xx2  ;
        by Job_code ;
           max_Rate_code=0;
 do n = 1  until (last.Job_code) ;
        if Annual_Rate> max_Rate_code
        then  do;
           max_Rate_code=Annual_Rate;
           end;
        output;
 end;
        run;

        proc print data=xx3;
        run;
/**********************************/
Thanks a lot.
Barrere.

 
 
 

DO LOOP Problem

Post by Dorfman, Pa » Sat, 16 Mar 2002 04:32:33


Hamani,

When you use the DOW-loop, the file-reading statement should be inside, not
outside, the loop. But for your problem, it is an overkill. If you have
sorted by Job_code, you may have sorted by Annual_rate as well. In this
case, the first record in the group will have the minimum value, and the
last one - the maximum value:

proc sort data = xx1 ;
   by job_code annual_rate ;
run ;

data xx2 (rename=(annual_rate=max_rate)) ;
   set xx1 ;
   by job_code ;
   retain min_rate ;
   if first.job_code then min_rate = annual_rate ;
   if last.job_code ;
run ;

The same done with the DOW-loop, if you are curious, would look like

data xx2 (rename=(annual_rate=max_rate)) ;
   do until (last.job_code) ;
      set xx1 ;
      by job_code ;
      if first.job_code then min_rate = annual_rate ;
   end ;
run ;

Better still, just use PROC SQL or PROC MEANS (SUMMARY). These are the tools
for the job, and you will not need to sort, either.

Kind regards,
======================
Paul M. Dorfman
Jacksonville, FL
======================

> -----Original Message-----

> Sent: Thursday, March 14, 2002 1:58 PM

> Subject: DO LOOP Problem

>     Hi there.
> I have the data:
>  Job_code         Annual_Rate;

> 0010                18000.00
> 0010                10000.00
> 0010                20000.00
> 0010                15000.00
> 0011                  8000.00
> 0011                12000.00
> 0012                15000.00

> I would have max and min for each Job_code, like this:

>                                   max_     min_
>                                              Annual_    Rate_    Rate_
>                           Obs    Job_code      Rate      code     code

>                            1        10        10000     20000    10000
>                            2        11        12000     12000     8000
>                            3        12        15000     15000    15000

> I wrote the following code, but It doesn't work. What's wrong?
> /**************************/
>         data xx1;
>         input Job_code    Annual_Rate;
>         datalines;
> 0010                18000.00
> 0010                10000.00
> 0010                20000.00
> 0010                15000.00
> 0011                  8000.00
> 0011                12000.00
> 0012                15000.00
> ;
> run;

>         proc sort data=xx1 out=xx2;
>         by Job_code ;
>         run;
>         /**************************/
>         data xx3 ;
>         retain Job_code  max_Rate_code n;
>     set xx2  ;
>         by Job_code ;
>            max_Rate_code=0;
>  do n = 1  until (last.Job_code) ;
>         if Annual_Rate> max_Rate_code
>         then  do;
>            max_Rate_code=Annual_Rate;
>            end;
>         output;
>  end;
>         run;

>         proc print data=xx3;
>         run;
> /**********************************/
> Thanks a lot.
> Barrere.

Blue Cross Blue Shield of Florida, Inc., and its subsidiary and
affiliate companies are not responsible for errors or omissions in this e-mail message. Any personal comments made in this e-mail do not reflect the views of Blue Cross Blue Shield of Florida, Inc.
 
 
 

1. MSN POP3 Loop-de-loop

 Again....maybe they're checking how far they can push. I predict a mass exodus if they do.;)

--
J. Dennis Russell, Sr.
Microsoft MVP
Please note:
Sandi Hardmeier/Nemesis and Zane Thomas/Sum One are *real* Microsoft MVPs.
To reply via e-mail, replace $$ with ss and replace notmail.com
with hotmail.com

2. CD driver for A2000 running 1.3WB and more

3. Loop d Loops

4. Palm IIIx prices

5. loop-d-loop

6. Windows 2000 - Xircom PCMCIA Drivers

7. ANYONE USING KBMS

8. gtk main loop and mico orb loop

9. % Loop limit expression too large for loop variable type.

10. For loop aliasing AND changing an array within a loop.

11. Basic Dos to Dos or Windows to Dos software

12. Do Loop Control Problem