- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi Reeza, so what is your solution for this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you Reeza
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you, this is great.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Max is 12
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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