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!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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