DATA Step, Macro, Functions and more

Concatenations not working as I thought in DO loop

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

Concatenations not working as I thought in DO loop

Having trouble getting a CAT function to work. I am trying to concatenate lines based on a ptr and lag_ptr. Here is my input and code

OPTIONS NOCENTER;                  

DATA RawData;                      

INPUT ptr lag_ptr full $5 lead $7;

datalines;                        

0 0 A B                            

0 0 B C                            

5 0 C D                            

5 5 D E                            

0 5 E F                            

0 0 F G                            

;                                  

 

DATA want; SET RawData;                  

FORMAT cat_line $CHAR05.; cat_line = " ";

IF ptr = 0 THEN DO;                      

   cat_line=full;                        

END;                                    

ELSE DO;                                

   IF lag_ptr = 0 THEN DO;              

     cat_line=CATT(full,lead);          

   END;                                  

   ELSE DO;                              

     cat_line=CATT(cat_line,lead);      

   END;                                  

END;                                    

IF lag_ptr > 0 THEN DELETE;              

RUN;                                    

 

PROC PRINT   DATA = want; VAR cat_line;

cat_line

A

B

CD

F                                                

 

This data is missing the next value “E”.

 What I wanted and have been trying to get is the data to look like this. AS you can see the E has been added to cat_line.

 

Cat_line

A

B

CDE

F

 

Any thoughts would be greatly appreciated, I have working on this for a while. Thank you!      


Accepted Solutions
Solution
2 weeks ago
Super User
Super User
Posts: 9,840

Re: Concatenations not working as I thought in DO loop

Please avoid coding all in uppercase, its like your shouting the code at us.  This works:

data rawdata;                      
 input ptr lag_ptr full $5 lead $7;
datalines;                        
0 0 A B                            
0 0 B C                            
5 0 C D                            
5 5 D E                            
0 5 E F                            
0 0 F G                            
;                                  
run;

data want; 
  set rawdata; 
  length cat_line $5;
  retain cat_line;
  if ptr=0 then cat_line=full;                        
  else do;                                
    if lag_ptr=0 then cat_line=cats(full,lead);          
    else cat_line=cats(cat_line,lead);      
  end;
/*  if lag_ptr > 0 then delete;           */   
run; 

Not sure what your intention for the if then delete is, as the row with CDE will be removed by this.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,389

Re: Concatenations not working as I thought in DO loop

Your condition for deletion eliminates the observation you want.

 

As a test, drop the "if ... then delete".  You'll see the observation you want, plus others you don't want.  Then decide what criterion you need to select only the wanted obs.

Contributor
Posts: 26

Re: Concatenations not working as I thought in DO loop

Removing the delete statement still does not produce the cat_line I am trying to end up with

cat_line
        
 A      
 B      
 CD     
 E      
 E      
 F      

 

I still need this

A

B

CDE

F

 

thx!

Solution
2 weeks ago
Super User
Super User
Posts: 9,840

Re: Concatenations not working as I thought in DO loop

Please avoid coding all in uppercase, its like your shouting the code at us.  This works:

data rawdata;                      
 input ptr lag_ptr full $5 lead $7;
datalines;                        
0 0 A B                            
0 0 B C                            
5 0 C D                            
5 5 D E                            
0 5 E F                            
0 0 F G                            
;                                  
run;

data want; 
  set rawdata; 
  length cat_line $5;
  retain cat_line;
  if ptr=0 then cat_line=full;                        
  else do;                                
    if lag_ptr=0 then cat_line=cats(full,lead);          
    else cat_line=cats(cat_line,lead);      
  end;
/*  if lag_ptr > 0 then delete;           */   
run; 

Not sure what your intention for the if then delete is, as the row with CDE will be removed by this.

Contributor
Posts: 26

Re: Concatenations not working as I thought in DO loop

Adding the retain did the trick, as far the delate statement is was looking at backwards. I need to drop the cat_line that does not contain the E. The final result is CDE needed to be concated and just CD is not needed any longer.

 

cat_line
       
 A     
 B     
 CD    
 CDE   
 E     
 F       

Contributor
Posts: 26

Re: Concatenations not working as I thought in DO loop

"Please avoid coding all in uppercase, its like your shouting the code at us."

 

It is not in all uppercase, key words that are required by SAS are in caps, anything that can change is in lowercase. This is the way I was taught and have always done it that way.

 

Sorry this bothers you.

Super User
Super User
Posts: 9,840

Re: Concatenations not working as I thought in DO loop

Nonsense.  There are no requirements by SAS for anything to be uppercase, only when doing text compares does case make any difference.  Your code is telling other people and the computer the story of your data and processing and should be highly readable by anyone.  

Contributor
Posts: 26

Re: Concatenations not working as I thought in DO loop

Thank you for your input, I always appreciate other points of view, I don’t recall where I said it was a requirement, because that would be non-sense. Funny thing is 90% of the all the code in my shop is in ALL CAPS, since I am coding on the mainframe is was the default for years. So… code in lower case looks incorrect to me. We can agree to disagree and keep our minds open to the way other people do things.

Thank you again, happy coding.

Super User
Posts: 13,930

Re: Concatenations not working as I thought in DO loop

You need to indicate WHICH E you are expecting in the output.

Since you have any record where lag_ptr > 0 deleted and both of the records in your example data with E as a value have lag_ptr=5 that is a significant issue with getting the state required output.

 

Also since the E appears to be likely from a different row than the one that concatenated C and D you need some sort of RETAIN to keep a value from row to row and an appropriate selection.

 

Perhaps a more verbal description of what this is supposed to be accomplishing might help as well as the entire contents of the output data set.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 77 views
  • 0 likes
  • 4 in conversation