## serparate a range of string variables into multiple varialbes using array

Solved
Regular Contributor
Posts: 182

# serparate a range of string variables into multiple varialbes using array

[ Edited ]

Hi!

Is it possible to change a range of variables (namepers1-namepers3)  currently a character string spearated by delimiter "/"

I was able to do it for one variable at a time...but for efficiency, is there a way to do Namepers1-namepsers3 in one datastep?

All the values in each variable are the same character string format...

data rr.namepers2006;
set sar_raw.rr2006 (keep=namepers1-namepers3) ;
i=1;
do while(scan(namepers1, i, "/") ne "");
new_var(i) =scan(namepers1, i, "/");
i+1;
end;
run;

thank you!

Accepted Solutions
Solution
‎07-31-2017 04:23 PM
Super User
Posts: 23,752

## Re: serparate a range of string variables into multiple varialbes using array

``````data want;;
infile cards dlm=',';
informat per1-per3 \$20.;
input per1 per2 per3;
array name(3) \$20. name1-Name3 ;
array rank(3) \$20. rank1-rank3;
array old_var(3) per1-per3;

do i=1 to 3;
name(i)=scan(old_var(i), 1, ".");
rank(i)=scan(old_var(i), 3, "/");
end;

cards;
Sample J ./0-3/ LT , Sample A ./0-1/ LT, Sample E ./0-4/ CDR
;

run;``````

All Replies
Posts: 3,040

## Re: serparate a range of string variables into multiple varialbes using array

although I'm not 100% sure I understand the problem, I believe the CATX function does what you want

but show us an example of the input, and what the output will look like for this situation

--
Paige Miller
Regular Contributor
Posts: 182

## Re: serparate a range of string variables into multiple varialbes using array

current data looks like this

 namepers1 namepers2 namepers3 Sample J ./0-3/ LT Sample A ./0-1/ LT Sample E ./0-4/ CDR

Want new colums to look like this

 last1 grade1 rank1 last2 grade2 rank2 last3 grade3 rank3 Sample J. 0-3 LT Sample A. 0-1 LT Sample E. 0-4 CDR
Posts: 3,040

## Re: serparate a range of string variables into multiple varialbes using array

You can use the SCAN function to separate the variables into "words" where the delimiter is a slash.

You'd want to put this into a data step loop, probably with namepers1 through namepers8 in an array.

--
Paige Miller
Regular Contributor
Posts: 182

## Re: serparate a range of string variables into multiple varialbes using array

I can get the san to work....just not with a range of variables.  I'm not soing something right...

data rr.namepers2006;
set sar_raw.rr2006  (keep=namepers1-namepers3);
array new_var(*)\$ name1-Name3 grade1-grade3 rank1-rank3;   *not sure if you can use array dimensions like this??;
i=1;
do while(scan(namepers1-namepers3, i, "/") ne "");
new_var(i) =scan(namepers1-namepers3, i, "/");          *trying to scan range of variables;
i+1;
end;
run;

Super User
Posts: 23,752

## Re: serparate a range of string variables into multiple varialbes using array

Does the number of items vary, or is it always 3?

Regular Contributor
Posts: 182

always 3
PROC Star
Posts: 8,164

## Re: serparate a range of string variables into multiple varialbes using array

Here is one way:

```data need (keep=id _name_ i data);
set rr2006;
array rec(*) \$ namepers1-namepers8;
array new_var(*) \$ last rank grade;
id=_n_;
do i=1 to dim(rec);
if rec(i) ne '' then do;
j=1;
do while(scan(rec(i), j, "/") ne "");
data =scan(rec(i), j, "/");
_name_=vname(new_var(j));
output;
j+1;
end;
end;
end;
run;

proc transpose data=need out=namepers2006 (drop=id _name_);
var data;
by id;
id _name_ i;
run;
```

Art, CEO, AnalystFinder.com

Super User
Posts: 23,752

## Re: serparate a range of string variables into multiple varialbes using array

@art297 solution is more dynamic, but if you only have 3, the code is about the same length overall. It's what you can understand enough to change
Solution
‎07-31-2017 04:23 PM
Super User
Posts: 23,752

## Re: serparate a range of string variables into multiple varialbes using array

``````data want;;
infile cards dlm=',';
informat per1-per3 \$20.;
input per1 per2 per3;
array name(3) \$20. name1-Name3 ;
array rank(3) \$20. rank1-rank3;
array old_var(3) per1-per3;

do i=1 to 3;
name(i)=scan(old_var(i), 1, ".");
rank(i)=scan(old_var(i), 3, "/");
end;

cards;
Sample J ./0-3/ LT , Sample A ./0-1/ LT, Sample E ./0-4/ CDR
;

run;``````
Regular Contributor
Posts: 182

## Re: serparate a range of string variables into multiple varialbes using array

@Reeza thank you!
☑ This topic is solved.