BookmarkSubscribeRSS Feed
Swapnil_21
Obsidian | Level 7

Hi,

 

I have data as below. 

Depending on the Month of account opening, I need to create new variable called Required_Sum as Opening Balance + sum of individual months greater than opening month

 

Desired is  :

For Cid=1 Account is opened in April month so Required_sum=opening_Balance_may19 + Balance_May19+ Balance_June19.... till end ----------->  Required_Sum =300+400 +200+150+80 = 1130

 

For Cid=2 Required_Sum=100+100+40+60=300

 

CidAccount_open_dateopening_Balance_apr19Balance_apr19Opening_balance_may19Balance_may19Opening_balance_jun19Balance_jun19Opening_balance_jul19Balance_jul19Opening_balance_aug19Balance_aug19
108-May-19..30040010020010015017080
213-Jun-19....1001001004010060
216-Jul-19.....150100100100100

\

 

Above is just sample data and there are columns from Apr15 till Sep19. How do I run it through macro and get only the first month opening balance. Any help is really appreciated a lot

 

5 REPLIES 5
Reeza
Super User
Are you required to maintain this data structure?
Did you create this table initially?
If you have your data in a long format it's easier to work with.
Swapnil_21
Obsidian | Level 7
Yes this table is required to be maintained. Every month 2 new columns are
added to this table.
Tom
Super User Tom
Super User

Not sure I get what you are trying to do, but shouldn't columns that represent dates before the starting date just by missing? In which case why not just sum ALL of the columns and ignore the value of the starting date.

 

How many numeric variables do you have? Make sure to count DATE variables since SAS stores dates as numbers.
How many of them need to contribute to the SUM?

data want ;
  set have;
  sum=sum(of _numeric_) - sum(of account_open_date sum);
run;

 

Reeza
Super User
Out of curiosity what would the sum of a 'balance' field represent from a business context?

In general, I keep $1000 in my account as balance. Not sure how the sum of that, $12,000 is a useful metric so I'm curious as to how this would be used...

Balance typically means the outstanding amount as of a particular date, but I suppose it could have a different interpretation.
hashman
Ammonite | Level 13

@Swapnil_21:

First, tell whomever has designed this "data structure" and intends to keep and maintain it this way: "Welcome to data processing and maintenance nightmare".  

 

Having said that, you can do what you need this way:

data have ;                                                                                                                                                                                                                                                     
  input Cid                                                                                                                                                                                                                                                     
        Account_open_date :date.                                                                                                                                                                                                                                
        opening_Balance_apr19                                                                                                                                                                                                                                   
        Balance_apr19                                                                                                                                                                                                                                           
        Opening_balance_may19                                                                                                                                                                                                                                   
        Balance_may19                                                                                                                                                                                                                                           
        Opening_balance_jun19                                                                                                                                                                                                                                   
        Balance_jun19                                                                                                                                                                                                                                           
        Opening_balance_jul19                                                                                                                                                                                                                                   
        Balance_jul19                                                                                                                                                                                                                                           
        Opening_balance_aug19                                                                                                                                                                                                                                   
        Balance_aug19                                                                                                                                                                                                                                           
  ;                                                                                                                                                                                                                                                             
  format Account_open_date yymmdd10. ;                                                                                                                                                                                                                          
  cards ;                                                                                                                                                                                                                                                       
1  08-May-19  .  .  300  400  100  200  100  150  170   80                                                                                                                                                                                                      
2  13-Jun-19  .  .    .    .  100  100  100   40  100   60                                                                                                                                                                                                      
2  16-Jul-19  .  .    .    .    .  150  100  100  100  100                                                                                                                                                                                                      
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
data want (drop = _:) ;                                                                                                                                                                                                                                         
  set have ;                                                                                                                                                                                                                                                    
  array nn _numeric_ ;                                                                                                                                                                                                                                          
  do over nn ;                                                                                                                                                                                                                                                  
    if cmiss (nn) then continue ;                                                                                                                                                                                                                               
    _open = lowcase (vname (nn)) =: "opening_balance" ;                                                                                                                                                                                                         
    _seq = sum (_seq, _open) ;                                                                                                                                                                                                                                  
    if _seq = 1 or (_seq and not _open) then Required_Sum = sum (required_sum, nn) ;                                                                                                                                                                            
  end ;                                                                                                                                                                                                                                                         
run ;                              

Kind regards

Paul D.

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
  • 5 replies
  • 1974 views
  • 0 likes
  • 4 in conversation