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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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