i am having data like this
tid tname$ time subt$
1 abc 20:12 xy,zx,yz
i want the above observation like this
tid tname$ time subt$
1 abc 20:12 xy
1 abc 20:12 zx
1 abc 20:12 yz
only code required?
That's not so hard:
data want;
set have;
length _subt $2;
do _N_=1 to countw(subt);
_subt=scan(subt,_N_,',');
output;
end;
drop subt;
rename _subt=subt;
run;
Here is an attempt to do this:
data have;
input tid tname$ time:time5. subt:$80.;
format time time5.;
datalines;
1 abc 20:12 xy,zx,yz
2 edf 20:12 aa,zx,yz,rb,rb
;
run;
proc sql noprint;
select max(countw(subt,',')) into: max_nb from have;
quit;
data have2;
set have;
nb = countw(subt,',');
array _subt(&max_nb) $ ;
do i=1 to nb;
subt_new = scan(subt,i,',');
output;
end;
drop i nb subt _subt:;
rename subt_new = subt;
run;
That's not so hard:
data want;
set have;
length _subt $2;
do _N_=1 to countw(subt);
_subt=scan(subt,_N_,',');
output;
end;
drop subt;
rename _subt=subt;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.