BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
slolay
Fluorite | Level 6

Hi All

Apologies for the semi-descriptive title...if any one can suggest a better one and I can change it then please let me know!

OK, to the problem, sounds an easy one but can;t think of an easy solution.

I have a dataset of subject and timepoint info.  

001     Screening

001     Visit 1

001     Visit 2

002     Screening

002     Visit 1

002     Visit 3

I need to create a new dataset where each subject in the output dataset has 4 records per timepoint? They have 4 tests done at each timepoint

Test1

Test2

Test3

Test4

So final dataset should look something like

001     Screening     Test1

001     Screening     Test2

001     Screening     Test3

001     Screening     Test4

001     Visit 1          Test1

001     Visit 1          Test2

001     Visit 1          Test3

001     Visit 1          Test4

....

....

....

Thanks!

Steve

The output data will be exported to excel and then filled in by hand

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

data have;

input id $ visit :$9.;

cards;

001     Screening

001     Visit1

001     Visit2

002     Screening

002     Visit1

002     Visit3

;

data want;

  set have;

  do test="test1", "test2", "test3", "test4";

  output;

  end;

proc print;

run;

View solution in original post

7 REPLIES 7
art297
Opal | Level 21

data want (drop=i);

  set have;

  do i=1 to 4;

    newvar='test'||put(i,1.);

    output;

  end;

run;

slolay
Fluorite | Level 6

Thanks Art

sorry the test1-test4 was an example

test could be like:

RBC

WBC

Urine

Platelets

Not in a sequential number as such

Apologies for the confusion

Steve

art297
Opal | Level 21

Then how about:

data want (drop=i _:);

  set have;

  array _new(4) $10. ('RBC' 'WBC' 'Urine' 'Platelets');

  do i=1 to 4;

    newvar=_new(i);

    output;

  end;

run;

slolay
Fluorite | Level 6

Thanks Linlin

Can you possibly expalin the logic here or tell me where in the SAS documentation I can read up on this.  I've not seen this do loop construction/use before.  Interesting.

Regards

Steve

art297
Opal | Level 21

Understood.  Linlin's suggested code was an example of one of the forms of an interative do loop.  It is documented at: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000201276.htm

However, in your case, you will also need to include a length statement or else the length will be determined by the first value in the list.  e.g.,

data want;

  set have;

  length test $10;

  do test='RBC','WBC','Urine','Platelets';

  output;

  end;

run;

slolay
Fluorite | Level 6

Thanks Art

Gonna go for Linlins option as a novel insight for me...and I don;t like arrays! :smileyshocked:

Linlin
Lapis Lazuli | Level 10

data have;

input id $ visit :$9.;

cards;

001     Screening

001     Visit1

001     Visit2

002     Screening

002     Visit1

002     Visit3

;

data want;

  set have;

  do test="test1", "test2", "test3", "test4";

  output;

  end;

proc print;

run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1946 views
  • 3 likes
  • 3 in conversation