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

Hi, 

I have a dataset like below  

Account                   Group                                            Amount                       NEED

A1                            Collected_202003                        100.00                         1

A1                            Vat_14_202003                             0.00                            1

A1                            Vat_15_202003                           15.00                            1

A1                            Collected_202004                         150.00                         2

A1                            Vat_14_202004                             0.00                             2

A1                            Vat_15_202004                           18.00                            2

B1                            Collected_202003                        100.00                          1

B1                            Vat_14_202003                             0.00                            1

B1                            Vat_15_202003                           15.00                            1

B1                            Collected_202004                         150.00                         2

B1                            Vat_14_202004                             0.00                            2

B1                            Vat_15_202004                           18.00                            2

 

 

I need the NEED column. 

What this will do is it will basically tell me that anything that ends with the first YYYYMM  (which is 202003 in this case) will be marked as 1 , the following month will be marked as 2 etc.  (The NEED columns is looking at what run month we are in , 1st, 2nd , 3rd etc.) .  I hope that makes sense ?  

 

Please will you try and assist me ? 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Please see the data step below in the preferred form of providing data. A data step that runs allows us to test code against the data. Paste code into a code box opened with the forum's </> or "running man" icon to preserve formatting. The message windows on this format will reformat code or log entries.

data have;
   input Account $ Group :$20.  Amount ;
datalines;
A1 Collected_202003 100.00 
A1 Vat_14_202003 0.00 
A1 Vat_15_202003 15.00 
A1 Collected_202004 150.00 
A1 Vat_14_202004 0.00 
A1 Vat_15_202004 18.00 
B1 Collected_202003 100.00 
B1 Vat_14_202003 0.00 
B1 Vat_15_202003 15.00 
;

data want;
   set have;
   by account;
   retain grpmon;
   if first.account then do;
   grpmon= input(substr(group,length(group)-5),yymmn6.);
   end;
   thismon =input(substr(group,length(group)-5),yymmn6.);
   need = intck('month',grpmon,thismon) +1;
   drop grpmon thismon;
run;

This assumes that the data is sorted as needed by the account. If the accounts are not actually in sort order but grouped correctly add NOTSORTED to the BY statement.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the <> icon or attached as text to show exactly what you have and that we can test code against.

View solution in original post

1 REPLY 1
ballardw
Super User

Please see the data step below in the preferred form of providing data. A data step that runs allows us to test code against the data. Paste code into a code box opened with the forum's </> or "running man" icon to preserve formatting. The message windows on this format will reformat code or log entries.

data have;
   input Account $ Group :$20.  Amount ;
datalines;
A1 Collected_202003 100.00 
A1 Vat_14_202003 0.00 
A1 Vat_15_202003 15.00 
A1 Collected_202004 150.00 
A1 Vat_14_202004 0.00 
A1 Vat_15_202004 18.00 
B1 Collected_202003 100.00 
B1 Vat_14_202003 0.00 
B1 Vat_15_202003 15.00 
;

data want;
   set have;
   by account;
   retain grpmon;
   if first.account then do;
   grpmon= input(substr(group,length(group)-5),yymmn6.);
   end;
   thismon =input(substr(group,length(group)-5),yymmn6.);
   need = intck('month',grpmon,thismon) +1;
   drop grpmon thismon;
run;

This assumes that the data is sorted as needed by the account. If the accounts are not actually in sort order but grouped correctly add NOTSORTED to the BY statement.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the <> icon or attached as text to show exactly what you have and that we can test code against.

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
  • 1 reply
  • 306 views
  • 0 likes
  • 2 in conversation