Help using Base SAS procedures

How to transfer this dataset?

Accepted Solution Solved
Reply
Contributor
Posts: 60
Accepted Solution

How to transfer this dataset?

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!


Accepted Solutions
Solution
‎08-03-2012 11:38 AM
Super Contributor
Posts: 1,636

Re: How to transfer this dataset?

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


All Replies
Solution
‎08-03-2012 11:38 AM
Super Contributor
Posts: 1,636

Re: How to transfer this dataset?

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;

Respected Advisor
Posts: 3,156

Re: How to transfer this dataset?

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

Super Contributor
Posts: 1,636

Re: How to transfer this dataset?

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

Contributor
Posts: 60

Re: How to transfer this dataset?

Many Thanks!

PROC Star
Posts: 7,467

Re: How to transfer this dataset?

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;

Contributor
Posts: 60

Re: How to transfer this dataset?

Thank you!

Contributor
Posts: 52

Re: How to transfer this dataset?

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;

Contributor
Posts: 60

Re: How to transfer this dataset?

Thank you!

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 298 views
  • 6 likes
  • 5 in conversation