DATA Step, Macro, Functions and more

How to Create a dataset with mutiple new Obs

Accepted Solution Solved
Reply
Contributor
Posts: 51
Accepted Solution

How to Create a dataset with mutiple new Obs

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


Accepted Solutions
Solution
‎11-17-2011 11:17 AM
Super Contributor
Posts: 1,636

Re: How to Create a dataset with mutiple new Obs

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


All Replies
PROC Star
Posts: 7,363

How to Create a dataset with mutiple new Obs

data want (drop=i);

  set have;

  do i=1 to 4;

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

    output;

  end;

run;

Contributor
Posts: 51

How to Create a dataset with mutiple new Obs

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

PROC Star
Posts: 7,363

How to Create a dataset with mutiple new Obs

Then how about:

data want (drop=i _Smiley Happy;

  set have;

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

  do i=1 to 4;

    newvar=_new(i);

    output;

  end;

run;

Contributor
Posts: 51

Re: How to Create a dataset with mutiple new Obs

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

PROC Star
Posts: 7,363

Re: How to Create a dataset with mutiple new Obs

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;

Contributor
Posts: 51

Re: How to Create a dataset with mutiple new Obs

Thanks Art

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

Solution
‎11-17-2011 11:17 AM
Super Contributor
Posts: 1,636

Re: How to Create a dataset with mutiple new Obs

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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