Calcite | Level 5

## creating variables out of a string

Hi,

I have a variable that store sequences

11100101

11001001

10011000

I want to divide those sequences into variables:

so the sequence 11100101 will be

var1=111

var2=0

var3=0

var4=1

var5=0

var6=1

and the sequence 11001001 will be

var1=11

var2=0

var3=0

var4=1

var5=0

var6=0

var7=1

and the sequence 10011000

var1 =1

var2=0

var3=0

var4=11

var5=0

var6=0

var7=0

How should I go about it?

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: creating variables out of a string

I assumed that since you used the word string then the variable is character.

data test;
input str :\$8.;

array v[8] \$8;
_i =
0;

do _s=1 by 0;
_r=findc(str||
' ',char(str,_s),'K',_s);
if _r eq 0 then leave;
if char(str,_s) eq '1' then do _i = _i + 1;
v[_i]=substrn(str,_s,_r-_s);

end;

else do _j = _s to _r-1;
_i +
1;
v[_i]=char(str,_j);

end;
_s=_r;

end;

drop _:;
cards;
11100101
11001001
10011000
111
1
0
00010100
11111111
00000000
;;;;
run;
12 REPLIES 12
PROC Star

## Re: creating variables out of a string

A good starting point would be to consider a few related questions ...

Is your original variable character or numeric?

Is it always 8 characters long?

Is the first character always "1"?

Should the new variables (var1 - var8) be character or numeric?

Super User

## Re: creating variables out of a string

Also, why is the first var1=111, the second var1=11, the third var1=1?

Why is there no var7 for the first value?

Why is var4=1 for the first and second but 11 for the third?

Calcite | Level 5

## Re: creating variables out of a string

The original is numeric but converted to char, it is not always 8 characters long and the first char is not always "1". the new var can be num or char...

the logic is to assemble one's that are near ech other together and zero can never be together. Kind of complicated...

Super User

## Re: creating variables out of a string

Not really, you just need to explain it, hard to guess what you want

BTW this seems a weird way to break things up, can you explain what you're going to use it for in the end?

Calcite | Level 5

## Re: creating variables out of a string

Hi Reeza, so what is your solution for this?

Super User

## Re: creating variables out of a string

data _null_ correct solution is for 8, so you can modify his. Mine is a bit more brute force.

data have;

input original \$12.;

cards;

11100101

11001001

10011000

111

1

0

00010100

11111111

00000000

10110001111

00000000000000000

;

data want;

set have;

array var (12) \$ var1-var12;

array str (12) \$ str1-str12;

j=1;

*separate into characters;

do i=1 to length(original) by 1;

str(i)=substr(original, i, 1);

end;

do k=1 to length(original);

if k=1 then var(j)=str(k);

else if str(k)=str(k-1) and str(k)="1" then var(j)=catt(var(j), str(k));

else do;

j+1;

var(j)=str(k);

end;

end;

drop str: i j k;

run;

Calcite | Level 5

Thank you Reeza

## Re: creating variables out of a string

I assumed that since you used the word string then the variable is character.

data test;
input str :\$8.;

array v[8] \$8;
_i =
0;

do _s=1 by 0;
_r=findc(str||
' ',char(str,_s),'K',_s);
if _r eq 0 then leave;
if char(str,_s) eq '1' then do _i = _i + 1;
v[_i]=substrn(str,_s,_r-_s);

end;

else do _j = _s to _r-1;
_i +
1;
v[_i]=char(str,_j);

end;
_s=_r;

end;

drop _:;
cards;
11100101
11001001
10011000
111
1
0
00010100
11111111
00000000
;;;;
run;
Calcite | Level 5

## Re: creating variables out of a string

Thank you, this is great.

PROC Star

## Re: creating variables out of a string

So the original is already converted to character, but may not be 8 characters long ... what is its maximum length?  After all, its maximum length determines the number of variables that we might have to create.

Calcite | Level 5

Max is 12

Super User

## Re: creating variables out of a string

```data test;
input str :\$8.;
cards;
11100101
11001001
10011000
111
1
0
00010100
11111111
00000000
;;;;
run;
data temp(drop=str i);
set test;
id=_n_;
do i=1 to length(str);
x=char(str,i);output;
end;
run;
data xx(drop=x);
set temp;
by id x notsorted;
length xx \$ 10;
retain xx;
xx=cats(xx,x);
if last.x then do;output;call missing(xx);end;
run;
data xxx(keep=id _xx);
set xx;
_xx=xx;
if find(xx,'0') then do;
do i=1 to length(xx);
_xx=char(xx,i);output;
end;
end;
else output;
run;
data xxx;
set xxx;
by id;
if first.id then n=0;
n+1;
run;
proc transpose data=xxx out=want(drop=_name_) prefix=var;
by id;
id n;
var _xx;
run;

```

Ksharp

Discussion stats
• 12 replies
• 2126 views
• 4 likes
• 6 in conversation