How to create sequential field values in an empty dataset

Reply
New Contributor
Posts: 4

How to create sequential field values in an empty dataset

Hello all and thank you in advance for any guidance!

I would like to use SAS to generate a dataset of fields as follows:

Site     Patient    
1          1001

1          1002

1          .....to 9999

2          1001

2          1002

2         ..... to 9999

.

.

99         .... to 9999

I want to basically create sequenced values for each field - site from 1-99 and patient from 1001-9999. Each site value would have patients in the range 1001-9999 up to 99 sites.

I tried many different ways but all seem to need data to exist already in order to make these. I can do this in excel but I know there must be a simple few lines of code to do this for me...

Thank you very much!

Super User
Posts: 19,767

Re: How to create sequential field values in an empty dataset

Basically, an empty dataset?

data want;

do Site=1 to 99;

do Patient=1001 to 9999;

output;

end;

end;

run;

Super User
Posts: 5,424

Re: How to create sequential field values in an empty dataset

If your Site and Patient id are truly sequential and all combinations are the same, just use do-loops in a data step with explicit output.

Data never sleeps
New Contributor
Posts: 4

Re: How to create sequential field values in an empty dataset

I did this (subset to smaller numbers for testing) and it worked. Thank you very much!

DATA T1;

retain siten 0 subject 1000;

  do i=1 to 10;

   Siten=siten+1;

  output;

  

    do t=1001 to 1010;  

   Subject=subject+1;

    output;

    end;

  end;

run; 

New Contributor
Posts: 4

Re: How to create sequential field values in an empty dataset

So I was wrong - it's almost right - any suggestions? I need subject to reset back to 1001 for each new site:

                                                      Obs    siten    subject    i      t

                                                           1      1        1001     1       .
                                                           2      1        1002     1    1001
                                                           3      1        1003     1    1002
                                                           4      1        1004     1    1003
                                                           5      1        1005     1    1004
                                                           6      1        1006     1    1005
                                                           7      1        1007     1    1006
                                                           8      1        1008     1    1007
                                                           9      1        1009     1    1008
                                                          10      1        1010     1    1009
                                                          11      1        1011     1    1010
                                                          12      2        1011     2    1011
                                                          13      2        1012     2    1001
                                                          14      2        1013     2    1002
                                                          15      2        1014     2    1003
                                                          16      2        1015     2    1004
                                                          17      2        1016     2    1005
                                                          18      2        1017     2    1006
                                                          19      2        1018     2    1007
                                                          20      2        1019     2    1008
                                                          21      2        1020     2    1009

Super User
Posts: 19,767

Re: How to create sequential field values in an empty dataset

See code in my solution above Smiley Happy

Super User
Super User
Posts: 7,035

Re: How to create sequential field values in an empty dataset

You have an extra OUTPUT statement.

New Contributor
Posts: 4

Re: How to create sequential field values in an empty dataset

Smiley Happy Reeza is right - simple code works perfectly. Thank you!

Ask a Question
Discussion stats
  • 7 replies
  • 284 views
  • 1 like
  • 4 in conversation