1 1 0 0 0 0 1 1 1
1 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1
1 1 1 1 1 1 0 1 1
Hi!
I am having some trouble trying to count the dummies as individual sequences seperated by the 0's (such as in the datalines above).
What I am trying to achieve, is have SAS count the dummies so that line 1 would count group 1 as having two elements, stop and group 2 as having three elements.
I.e.:
1_1 1_2 0 0 0 0 2_1 2_2 2_3
I have tried to do this by generating the lines as arrays and looping through the groups with the "do until" function.
However, I am wondering if there is any do-loop function that would allow me to loop "from" a point mid-array, so that I could begin a second loop from "2_1"?
I hope this makes sense, any help is hugely appreciated!
Thanks in advance. 🙂
how about this code?
data have;
input a1-a9;
datalines;
1 1 0 0 0 0 1 1 1
1 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1
1 1 1 1 1 1 0 1 1
;
run;
data want;
set have;
length result $200;
array a{9};
idx1=1;
idx2=1;
do i=1 to dim(a);
if a{i}=1 then do;
result=catx(' ',result,catx('_',idx1,idx2));
idx2+1;
end;
if a{i}=0 then do;
result=catx(' ',result,0);
if i>1 then do;
if not a{i-1}=0 then do;
idx1+1;
idx2=1;
end;
end;
end;
end;
drop idx: i;
run;
Results is follow.
how about this code?
data have;
input a1-a9;
datalines;
1 1 0 0 0 0 1 1 1
1 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1
1 1 1 1 1 1 0 1 1
;
run;
data want;
set have;
length result $200;
array a{9};
idx1=1;
idx2=1;
do i=1 to dim(a);
if a{i}=1 then do;
result=catx(' ',result,catx('_',idx1,idx2));
idx2+1;
end;
if a{i}=0 then do;
result=catx(' ',result,0);
if i>1 then do;
if not a{i-1}=0 then do;
idx1+1;
idx2=1;
end;
end;
end;
end;
drop idx: i;
run;
Results is follow.
You may want to consider a long formatted data instead, I suspect it will be more useful in the long run.
data have;
input a1-a9;
ID=_n_;
datalines;
1 1 0 0 0 0 1 1 1
1 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1
1 1 1 1 1 1 0 1 1
;
run;
proc transpose data=have out=long;
by ID;
var a1-a9;
run;
data long_indexed;
set long;
by ID col1 notsorted;
if first.ID then
do;
group_cnt=0;
sub_index=0;
end;
if first.col1 and col1 then
do;
group_cnt+1;
sub_index=0;
end;
sub_index+1;
if col1=1 then
index=catx("_", group_cnt, sub_index);
else
index=0;
run;
proc transpose data=long_indexed;
by ID;
var index;
run;
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: