BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MLautrup
Calcite | Level 5
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. 🙂

 

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

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.

2021-06-11_19h43_19.png

View solution in original post

3 REPLIES 3
japelin
Rhodochrosite | Level 12

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.

2021-06-11_19h43_19.png

MLautrup
Calcite | Level 5
That did the trick, thank you so much!
Reeza
Super User

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 505 views
  • 1 like
  • 3 in conversation