DATA Step, Macro, Functions and more

parsing a string using data as new variable name

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

parsing a string using data as new variable name

[ Edited ]

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

 


Accepted Solutions
Solution
‎09-15-2016 02:06 PM
Super User
Posts: 17,784

Re: parsing a string using data as new variable name

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. 

View solution in original post


All Replies
Solution
‎09-15-2016 02:06 PM
Super User
Posts: 17,784

Re: parsing a string using data as new variable name

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. 

New Contributor
Posts: 3

Re: parsing a string using data as new variable name

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 256 views
  • 0 likes
  • 2 in conversation