+ Reply to Thread
Results 1 to 3 of 3

Thread: Retain and auto increment in proc sql

  1. #1
    Points: 373, Level: 7
    Level completed: 46%, Points required for next Level: 27

    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Retain and auto increment in proc sql



    Hi ,

    i want to generate sequence number with respect to some key variables. we can genrate this in DATA STEP IN sas using sorting and first.by variable, is there any possible to do the derivation PROC SQL, Please help me

    student mark
    01 15
    01 04
    01 11
    02 09
    02 02
    02 15
    03 01
    03 15
    03 24


    i want to get output as
    student mark seq
    01 15 3
    01 04 1
    01 11 2
    02 09 2
    02 02 1
    02 15 3
    03 01 1
    03 15 2
    03 24 3

  2. #2
    Points: 2,180, Level: 28
    Level completed: 20%, Points required for next Level: 120

    Location
    Chicago, IL
    Posts
    108
    Thanks
    1
    Thanked 19 Times in 15 Posts

    Re: Retain and auto increment in proc sql

    Try this:

    Code: 
    data student;
    input student mark;
    datalines;
    01 15
    01 04
    01 11
    02 09
    02 02
    02 15
    03 01
    03 15
    03 24
    run;
    
    proc sort data=student; by student mark; run;
    
    data student2; set student;
    by student;
    if first.student then do;
    seq0 = 1;
    output;
    end;
    run;
    
    data student3;
    merge student (in=a) student2 (in=b);
    by student mark;
    if a;
    if a and not b then seq0=0;
    run;
    
    data student_final; set student3;
    retain seq;
    if seq0 ne 0 then seq=seq0;
    	else seq=seq+1;
    drop seq0;
    run;

  3. #3
    Points: 2,180, Level: 28
    Level completed: 20%, Points required for next Level: 120

    Location
    Chicago, IL
    Posts
    108
    Thanks
    1
    Thanked 19 Times in 15 Posts

    Re: Retain and auto increment in proc sql


    Hmm, didn't read your post carefully enough. I will consider whether proc sql can be used.

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts








Advertise on Talk Stats