Hello,
I would like to get the seperated subgroups Result1-Result3 from the column 'Name' when the numbers are separated by _. Thanks.
data datain1;
infile datalines dsd;
input Name : $300. Result1 : $100. Result2 : $100. Result3 : $100.;
datalines;
5648_6666, 5648, 6666,
000_9463, 000, 9463,
000_4721_444, 000, 4721, 444
4721_00_999, 4721, 00, 999
;
run;
You can use the SCAN() function along with the COUNTW() function, with the underscore as the delimiter, in a loop.
I'm feeling generous. Here's one example of @PaigeMiller's suggestion.
Note that I am only reading your Name variable.
data datain1;
infile datalines dsd;
input Name : $300. ;
array result (3) $100;
do i=1 to countw(name,'_');
result[i] = scan(name,i,'_');
end;
drop i;
datalines;
5648_6666, 5648, 6666,
000_9463, 000, 9463,
000_4721_444, 000, 4721, 444
4721_00_999, 4721, 00, 999
;
run;
Caveat: To create the Array you really need to know the maximum number of values you might encounter. If you declare the array for 3 as in the example and one of the name values was 12_34_56_78 then Countw will return 4 words and attempt to loop to the 4th element of the result array. Which was not defined, only 3.
Also consecutive _ may not do exactly what you want. Test it and see with a value like 123__456 (that is two consecutive underscores). Another case to consider is behavior if the first or last character is an _.
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 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.