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

 

Hello,

Have a dataset that has an ID column, an item column and column called gap that is created to show the gap in days between the item columns per ID.

 

I have code where I want to create a string (like a DNA string) of the item by each ID. So for each ID I would create a column called string that would concatenate all the item column values into a long string.

For example for ID 1 (in the data have below) the final string would yield O-O-O-O-O-T and for ID 2 the final string would be O-O-O-O-O-T. I know how to create the above string (see my code below).

What I want to do is bring in the condition that if the gap is greater than 10 then to start the string building anew.

so my output would actually look like this:

 

Any ideas on what to add to my code below?

 

What I would want is:

ID string

1   O-O-O

1   O-O-T

2   O-O-O

2   O-O-T

 

Here is a sample of creating the have dataset.

 

 

data have; 
input ID 1. item $1. gap 5.; 
datalines; 
1 O 3 
1 O 4 
1 O 5 
1 O 15 
1 O 3 
1 T 4 
2 O 3 
2 O 5 
2 O 7 
2 O 11 
2 O 3 
2 T O 
; run;

 

 

In this code I can create the string by ID, but how do I add the condition that if the gap is greater than 10 to start building a new string?

 

data want; 
set have; 
by ID; 
retain O T string;
length O T $1. string $50. ; 
if first.ID then do; 
O='0'; T='0';string=' ';end; 
string=catx('-',string,item); 
if item='O' then O ='1';
else if item='T' then T='1'; 
run;

 

What I would want is:

ID string

1   O-O-O

1   O-O-T

2   O-O-O

2   O-O-T

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have ;                            
 input ID item :$1. gap ;              
 cards ;                               
1 O 3                                  
1 O 4                                  
1 O 5                                  
1 O 15                                 
1 O 3                                  
1 T 4                                  
2 O 3                                  
2 O 5                                  
2 O 7                                  
2 O 11                                 
2 O 3                                  
2 T 0                                  
;                                      
run ; 
data temp;
 set have;
 by id;
 if first.id or gap>10 then group+1;
run;
data want;
length want $ 200;
 do until(last.group);
   set temp;
   by group;
   want=catx('-',want,item);
 end;
 drop gap item group;
run;

View solution in original post

6 REPLIES 6
ScottBass
Rhodochrosite | Level 12

Edit your post, pasting the code using the Insert SAS Code icon.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
heffo
Pyrite | Level 9

Your first data step creates some empty variables, but that is easy fixed. So, for the next step there is a bit of if cases in the end. I assume that you only want to output the last row for each ID or if gap is larger than 10. 

data have;
	length ID 8 item $ 1 gap 8;
	input ID item gap ;
	datalines;
1 O 3 
1 O 4 
1 O 5 
1 O 15 
1 O 3 
1 T 4 
2 O 3 
2 O 5 
2 O 7 
2 O 11 
2 O 3 
2 T O 
;;;
run;

data want;
	set have;
	by ID;
	retain O T string;
	length O T $1. string $50.;

	if first.ID then do;
		O='0';
		T='0';
		string=' ';
	end;


	if item='O' then
		O ='1';
	else if item='T' then
		T='1';
	if last.ID then do;
*If this is the last row with this ID, then add item to the string then output it.; string=catx('-',string,item); output; end; else if gap > 10 then do; *If the gap is larger than 10, then output it first, then "reinit" string with item.
; output; string=item; end; else string=catx('-',string,item);
*If neither of those cases, add item to string and then, without output, read the next line. ; run;
hashman
Ammonite | Level 13

@sas_student1 :

Looks like a veritable DoW-loop job:

data have ;                            
 input ID item :$1. gap ;              
 cards ;                               
1 O 3                                  
1 O 4                                  
1 O 5                                  
1 O 15                                 
1 O 3                                  
1 T 4                                  
2 O 3                                  
2 O 5                                  
2 O 7                                  
2 O 11                                 
2 O 3                                  
2 T 0                                  
;                                      
run ;                                  
                                       
data want (keep = id string) ;         
  do until (last.id) ;                 
    set have ;                         
    by id ;                            
    length string $ 16 ;               
    if gap > 10 then do ;              
      output ;                         
      string = "" ;                    
    end ;                              
    string = catx ("-", string, item) ;
  end ;                                
  output ;                             
run ;                                  

Kind regards

Paul D.

Ksharp
Super User
data have ;                            
 input ID item :$1. gap ;              
 cards ;                               
1 O 3                                  
1 O 4                                  
1 O 5                                  
1 O 15                                 
1 O 3                                  
1 T 4                                  
2 O 3                                  
2 O 5                                  
2 O 7                                  
2 O 11                                 
2 O 3                                  
2 T 0                                  
;                                      
run ; 
data temp;
 set have;
 by id;
 if first.id or gap>10 then group+1;
run;
data want;
length want $ 200;
 do until(last.group);
   set temp;
   by group;
   want=catx('-',want,item);
 end;
 drop gap item group;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 688 views
  • 2 likes
  • 5 in conversation