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!
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;
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;
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
Thank you Haikuo! I will use it in the future.
Many Thanks!
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;
Thank you!
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;
Thank you!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.