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?
I assumed that since you used the word string then the variable is character.
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?
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?
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...
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?
Hi Reeza, so what is your solution for this?
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;
Thank you Reeza
I assumed that since you used the word string then the variable is character.
Thank you, this is great.
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.
Max is 12
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.