Below is the dataset that I have:
I want to transform it like the data below:
I tried the code below but it isn't working:
data want;
set ref_table;
i=1;
do until (missing(pre_req));
pre_req=scan(pre_req,i,",");
i+1;
end;
run;
May I know how to modify this?
You are overwriting pre_req in the loop, that can't work as expected. You need to extract the values to a new variable. Code is untested.
data want;
set ref_table;
do i = 1 to countw(pre_req, ',');
new_pre_req = scan(pre_req, i, ",");
output;
end;
drop pre_req;
rename new_pre_req = pre_req;
run;
You are overwriting pre_req in the loop, that can't work as expected. You need to extract the values to a new variable. Code is untested.
data want;
set ref_table;
do i = 1 to countw(pre_req, ',');
new_pre_req = scan(pre_req, i, ",");
output;
end;
drop pre_req;
rename new_pre_req = pre_req;
run;
use next code:
data want;
set ref_table(rename=(pre_reg=reqs));
do i=1 to countw(reqs);
pre_req = scan(reqs,i,",");
outputl
end;
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.