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

Hi,

I have a dataset like this:

data A;

    input code $ type $12.;

    cards;

    ASD1    6,8,14

    ASD2    14

    ASD3    4,8,14

    ASD4    4,8,14

    ASD5    14

    ASD6    1,2,4,8,11

    ASD7    5

    ASD8    2,5

run;

How can I transfer this dataset to following form:

ASD1 6

ASD1 8

ASD1 14

ASD2 14

.

.

.

ASD8 2

ASD8 5

Thanks in advance for any help!

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

data A;
    input code $ type $12.;
    cards;
    ASD1    6,8,14
    ASD2    14
    ASD3    4,8,14
    ASD4    4,8,14
    ASD5    14
    ASD6    1,2,4,8,11
    ASD7    5
    ASD8    2,5
;

data want(keep=code new);
  set a;
  do i=1 to 99 while (scan(type,i) ne ' ');
  new=input(scan(type,i),2.);
  output;
  end;
  run;

proc print;run;

View solution in original post

8 REPLIES 8
Linlin
Lapis Lazuli | Level 10

data A;
    input code $ type $12.;
    cards;
    ASD1    6,8,14
    ASD2    14
    ASD3    4,8,14
    ASD4    4,8,14
    ASD5    14
    ASD6    1,2,4,8,11
    ASD7    5
    ASD8    2,5
;

data want(keep=code new);
  set a;
  do i=1 to 99 while (scan(type,i) ne ' ');
  new=input(scan(type,i),2.);
  output;
  end;
  run;

proc print;run;

Haikuo
Onyx | Level 15

LinLin,

How about removing the rigid part of '99' and using 'by 1'?

data want(keep=code new);

  set a;

  do i=1 by 1 while (scan(type,i) ne ' ');

  new=input(scan(type,i),2.);

  output;

  end;

  run;

Haikuo

Linlin
Lapis Lazuli | Level 10

Thank you Haikuo! I will use it in the future.

rpg163
Calcite | Level 5

Many Thanks!

art297
Opal | Level 21

Basically, the same suggestion as Linlin, but I'd make i a counter rather than a loop.  i.e.:

data b (keep=code type);

  set a (rename=(type=type_in));

  i=1;

  do while (scan(type_in,i,",") ne "");

    type=input(scan(type_in,i,","),2.);

    output;

    i+1;

  end;

run;

rpg163
Calcite | Level 5

Thank you!

tish
Calcite | Level 5

LOL I'd loop it and go to the number of commas plus 1. I left in some commented code for the possibility that you want the value to be character, not numeric.

data B (keep=code value);

   set A;

  

/* length value $ 2;    */

  

   do i = 1 to countc(type, ',')+1;

/*    value = scan(type, i);    */

      value = input(scan(type, i), 2.0);

      output;

   end;

run;

rpg163
Calcite | Level 5

Thank you!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 2242 views
  • 6 likes
  • 5 in conversation