BookmarkSubscribeRSS Feed
Ashwini
Calcite | Level 5

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

3 REPLIES 3
Tom
Super User Tom
Super User

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;

art297
Opal | Level 21

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;

Ashwini
Calcite | Level 5

Thanks Tom and Art a lot for your kind information.

Regards,

Ashwini

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
  • 3 replies
  • 778 views
  • 0 likes
  • 3 in conversation