DATA Step, Macro, Functions and more

create multiple dataset from one dataset

Reply
Frequent Contributor
Posts: 76

create multiple dataset from one dataset

I have one dataset like student data

name    english science history math

ram         65        48        89      49

Hari         79        69       90      98

gopal       87        35      30       90

I have to create 3 dataset like ram , hari ,gopal

Ram dataset contail like below

english 56

science 48

History 89

hari and gopal dataset are same as ram

Kindly help me .

Thanks,

Regards,

Ashwini

Super User
Super User
Posts: 6,500

Re: create multiple dataset from one dataset

You can use PROC TRANSPOSE to rotate the grades into rows.

Splitting into individual datasets is harder.  But we can query the original dataset and generate some code into macro variables to make it a little easier.

Are you sure you need the individual files?  Why not just use the single rotated dataset and use a BY statement to process it by NAME?

data have;

  input name $ english science history math;

cards;

ram   65 48 89 49

Hari  79 69 90 98

gopal 87 35 30 90

;;;;

proc transpose data=have out=want(rename=(_name_=course col1=grade));

  by name notsorted ;

run;

proc sql noprint ;

  select 'grade_'||name

       , 'if name='||quote(name)||' then output grade_'||name

    into :dsnames separated by ' '

       , :ifstatement separated by ';'

    from have

  ;

quit;

data &dsnames;

  set want ;

  &ifstatement ;

run;

PROC Star
Posts: 7,363

Re: create multiple dataset from one dataset

Another approach, after one transposes the data, is to use a hash to create the separate files.  E.g., I think that the following produces the files you want:

data have;

  input name $ english science history math;

cards;

ram   65 48 89 49

Hari  79 69 90 98

gopal 87 35 30 90

;

proc transpose data=have out=temp(rename=(_name_=course col1=grade));

  by name notsorted ;

run;

data temp (index = (name));

    set temp;

run;

data _null_;

  dcl hash hh   (             );

  hh.definekey  ('k'         );

  hh.definedata ('name','course','grade');

  hh.definedone ();

  do k = 1 by 1 until (last.name);

     set temp;

     by name;

     hh.add ();

  end;

  hh.output (dataset: name);

run;

Frequent Contributor
Posts: 76

Re: create multiple dataset from one dataset

Thanks Tom and Art a lot for your kind information.

Regards,

Ashwini

Ask a Question
Discussion stats
  • 3 replies
  • 165 views
  • 0 likes
  • 3 in conversation