I need some help with what I think is simple but I cannot seem to get it to work or find anyone else posting this...I can parse a text string by array but I want to identify the number of the parsed string in the new variable name (match them up).
I have lots of data fields that have selected more than one option and they are separated by commas. Since I have alot of these fields in my data I was hoping to find a less manual way of doing this. I want data to correspond to the variable name. Obviously, it is a character variable source but the source1-source8 I want as numeric.
variable source has values
1,3,7
1,2,5
1,2,3,6,7
1
2,8
and so on.
I want to separate by commas data into 8 variables (source1, source2...source8). But I want the '1,3,7, to show up in source1 as '1', source3 as '3', source7 as '7' and not source1 as '1', source2 as '3', and source3 as '7'.
Thanks, JJ
Do you know ahead of time that you will have 8 values?
If so, declare an array and use scan to parse out the text.
COUNTW - Number of items in list
SCAN - extracts specific item
INPUT - convert char to numeric
SOURCE(num) -> Array to assign value using it as an index.
array source(8) source1-source8;
n_items=countw(string);
do i=1 to n_items;
x=scan(string, i);
x_num=input(x, 8.);
source(x_num)=x_num;
end;
If you don't know how many values you'll have ahead of time this doesn't work as well. Depending on the size of the data, its probably easiest to figure out the maximum number and use that in this process.
Do you know ahead of time that you will have 8 values?
If so, declare an array and use scan to parse out the text.
COUNTW - Number of items in list
SCAN - extracts specific item
INPUT - convert char to numeric
SOURCE(num) -> Array to assign value using it as an index.
array source(8) source1-source8;
n_items=countw(string);
do i=1 to n_items;
x=scan(string, i);
x_num=input(x, 8.);
source(x_num)=x_num;
end;
If you don't know how many values you'll have ahead of time this doesn't work as well. Depending on the size of the data, its probably easiest to figure out the maximum number and use that in this process.
Thank you Reeza! I will know the items since it was a category (check all apply thing) and it worked like a charm. Data sets are not huge. I will use this often. Yeehaw! JJ
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.