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!
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.
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.