data one;
length id val $200;
input id val ;
datalines;
1 3,8,9,8
2 3,8,9,8
;
run;
/*output should be
1 3
1 8
1 9
1 8
2 3
2 8
2 9
2 8
*/
I tried using the scan function but it doesn't work. I am trying to separate the values after the commas to a new variable as listed above
data one;
length id val $200;
input id val ;
datalines;
1 3,8,9,8
2 3,8,9,8
;
run;
data want;
set one;
temp=compress(val,',');
do n=1 to length(temp);
want=char(temp,n);
output;
end;
keep id want;
run;
If you tried using the SCAN function and it doesn't work ... show us what you did. Show us the SASLOG so we can see your code and error messages.
HINT: Don't ever tell us "it doesn't work" without additional explanation because that's not enough information for us to help you. Explain! Show us the SASLOG.
data test;
set one;
new_var = compress(val,',');
run;
this is what I got to work, it removes the commas but I was trying to use a do loop to get it to populate into a 4 different obs.
data one;
length id val $200;
input id val ;
datalines;
1 3,8,9,8
2 3,8,9,8
;
run;
data want;
set one;
temp=compress(val,',');
do n=1 to length(temp);
want=char(temp,n);
output;
end;
keep id want;
run;
That's awesome!! works great!
Thats overthinking it. A simple change to the import program eliminates the need for post processing at all:
data one; infile datalines dlm=" ,"; length id val1 val2 val3 val4 8; input id val1--val4; datalines; 1 3,8,9,8 2 3,8,9,8 ; run;
@chrissowden If you are reading an external file and not SAS dataset, I think @RW9 solution is right. However if you are reading a SAS dataset, you can stick to mine and tweak it if need be
So the solution by @novinosrin seems to work if you can guarantee that the numbers in the original data set are integers between 0 and 9. It will not work if the numbers are 10 or higher; it will not work for negative numbers and it will not work for non-integers.
@PaigeMiller Thank you for waking me up.
Revised to address -ve and higher digits
data one;
length id val $200;
input id val ;
datalines;
1 -3,8888,9,-80
2 39,88,9,8
;
run;
data want;
set one;
length want $20.;
do n=1 to countw(val,',');
want=scan(val,n,',');
output;
end;
keep id want;
run;
So you are not going to show us the SASLOG for the problem you had with the SCAN function? You don't need help with that any more?
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 lock in 2025 pricing—just $495!
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.