BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
chrissowden
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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;

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
chrissowden
Obsidian | Level 7

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.

 

novinosrin
Tourmaline | Level 20
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;
chrissowden
Obsidian | Level 7

That's awesome!! works great!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
novinosrin
Tourmaline | Level 20

@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

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
novinosrin
Tourmaline | Level 20

@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;
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1497 views
  • 2 likes
  • 4 in conversation