BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasuser1000
Calcite | Level 5

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
data_null__
Jade | Level 19

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;

View solution in original post

12 REPLIES 12
Astounding
PROC Star

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?

ballardw
Super User

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?

sasuser1000
Calcite | Level 5

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...

Reeza
Super User

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

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

sasuser1000
Calcite | Level 5

Hi Reeza, so what is your solution for this?

Reeza
Super User

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;

sasuser1000
Calcite | Level 5

Thank you Reeza

data_null__
Jade | Level 19

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;
sasuser1000
Calcite | Level 5

Thank you, this is great.

Astounding
PROC Star

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.

sasuser1000
Calcite | Level 5

Max is 12

Ksharp
Super User
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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