BookmarkSubscribeRSS Feed
PrudhviB
Obsidian | Level 7

Hi this is my code 

PROC SQL;
   CREATE TABLE WORK.Jul2019 AS 
   SELECT t1.MonthYear, 
          t1.FIN_PROD_GRP_CD, 
          t1.'Ref Code'n AS 'Previous Cycle'n, 
          t3.'Ref Code'n AS 'Current Cycle'n, 
          /* Acct Count */
            (COUNT(t3.'Ref Code'n)) AS 'Acct Count'n
      FROM WORK.ALL_INVENTORY t1
           LEFT JOIN NCDSEEXP.V_COLN_ACC_HIST_DLINQ_BCKT t2 ON (t1.ACC_KEY = t2.ACC_KEY) AND (t1.ACC_KEY_SEQ = 
          t2.ACC_KEY_SEQ)
           LEFT JOIN WORK.'REFERENCE TBL'n t3 ON (t2.ACC_PROD_DLINQ_BCKT_CD = t3.'Dlq Code'n)
      WHERE t1.MonthYear = '31Jul2019'd AND (1882828800) >= t2.ACC_PROD_DLINQ_BCKT_START_DATE AND (1882828800) < 
           t2.ACC_PROD_DLINQ_BCKT_END_DATE
      GROUP BY t1.MonthYear,
               t1.FIN_PROD_GRP_CD,
               t1.'Ref Code'n,
               t3.'Ref Code'n
      ORDER BY t1.'Ref Code'n,
               t3.'Ref Code'n;
run;

basically i have my date range from 201711-202001

 

and i want to loop all the date values in the code. please assist. thank you for your help in advance. 

 

9 REPLIES 9
ballardw
Super User

Since SQL doesn't actually have any "loop" constructs you might be better off actually showing some small examples of the data that you have and the desired result.

 

Likely join the data and then use a data step to loop over a given values with a DO loop. But without knowing what you are actually attempting to create I'm not going to attempt to provide any code.

PrudhviB
Obsidian | Level 7

Hi Ballardw,

this is my data 

 

MonthYear (MMYYS7.)

201711

201712

201801

.

.

.

.

202001


i want to use the monthYear (ranging from 201711-202001) col from this table to create a table names for each value for example :

all the dates with 201711 will be under NOV_2017 and 201805 will be under MAY_2018 

 

and the date time value is next month end date. for instance in the code i have it for july2019 the date time value is 31st aug 2019 (1882828800). 

 

so i want to create tables from 201711 till date (which is 202001, approx 27 tables).

eventually i want the output as shown in the image.

Image.JPG

PaigeMiller
Diamond | Level 26

You would be much better off with a single data set containing all months. This would be far less programming, and also run faster. Why make your life so complicated?? Analyses can be done BY MONTHYEAR and no looping is needed.

 

PROC SQL;
   CREATE TABLE WORK.ALL AS 
   SELECT t1.MonthYear format=yymm5., 
          t1.FIN_PROD_GRP_CD, 
          t1.'Ref Code'n AS 'Previous Cycle'n, 
          t3.'Ref Code'n AS 'Current Cycle'n, 
          /* Acct Count */
            (COUNT(t3.'Ref Code'n)) AS 'Acct Count'n
      FROM WORK.ALL_INVENTORY t1
           LEFT JOIN NCDSEEXP.V_COLN_ACC_HIST_DLINQ_BCKT t2 ON (t1.ACC_KEY = t2.ACC_KEY) AND (t1.ACC_KEY_SEQ = 
          t2.ACC_KEY_SEQ)
           LEFT JOIN WORK.'REFERENCE TBL'n t3 ON (t2.ACC_PROD_DLINQ_BCKT_CD = t3.'Dlq Code'n)
      WHERE t1.MonthYear > '30NOV2017'd AND (1882828800) >= t2.ACC_PROD_DLINQ_BCKT_START_DATE AND (1882828800) < 
           t2.ACC_PROD_DLINQ_BCKT_END_DATE
      GROUP BY t1.MonthYear,
               t1.FIN_PROD_GRP_CD,
               t1.'Ref Code'n,
               t3.'Ref Code'n
      ORDER BY t1.'Ref Code'n,
               t3.'Ref Code'n;
run;

By the way, it would really be helpful if you explained how your desired month range of 201711 to 202001 relates to your code, as I requested earlier. I have made a guess, but I don't really know if I put the right things in the right places. Please explain this.

 

--
Paige Miller
PrudhviB
Obsidian | Level 7

Hey Miller,

 

yes i agree analysis by month makes my life much better. let me give you the gist, 

 

i am building my main table to represent roll rates for collections, which will have current cycle and previous cycle for the same accounts. 

so my logic is break my data into individual months ( which will have monthyear and current cycle values) and join this individual month data back to the bucket tbl (historical data of all delinquencies accounts) to get the previous cycle values ( this is where i use date time value) and then append all the months to get the over all view of roll rates. ( i dont know if there is a better logic i can use to get the previous bucket) .

 

it is so hard to provide the actual data let me know if this sample makes sense. 

 

end data looks like 

 

 

Month-YearProduct GrpPrevious CycleCurrent Cycle# of accounts
Jul-19Grp 1 1060
Jul-19Grp 21023155
Jul-19Grp 310736
Jul-19Grp 4107704
Jul-19Grp 5101436
Jul-19Grp 611783
Jul-19Grp 1 1110148
Jul-19Grp 2118494
Jul-19Grp 3119
Jul-19Grp 411699
Jul-19Grp 5112+25
Jul-19Grp 61210128
Jul-19Grp 1 122850
Jul-19Grp 212415
Jul-19Grp 312860
Jul-19Grp 4126899
Jul-19Grp 513255
Jul-19Grp 6131
Jul-19Grp 1 1374
Jul-19Grp 2137
Jul-19Grp 313110
Jul-19Grp 4155
Jul-19Grp 5161
Jul-19Grp 61007
Jul-19Grp 1 1011
Jul-19Grp 210101
Jul-19Grp 310116
Jul-19Grp 4101165
Jul-19Grp 510113
Jul-19Grp 610113
Jul-19Grp 1 10126
Jul-19Grp 21021
Patrick
Opal | Level 21

Couldn't you just join the table with itself using a join condition like

l.date=intnx('month',r.date,-1)

PrudhviB
Obsidian | Level 7

i tried this but this will not help me as i am trying to get the snapshot (Bucket cycle) of the same account in the previous month. 

Patrick
Opal | Level 21

Please try and provide some sample HAVE data, then explain the logic and show us the desired result.

And please provide the sample data via a working SAS data step so we can spend our time for answering your question and not for preparing the sample data.

PaigeMiller
Diamond | Level 26

@Patrick wrote:

Couldn't you just join the table with itself using a join condition like

l.date=intnx('month',r.date,-1)


Yes, absolutely, something like this would work and allow one data set to have everything needed that @PrudhviB described. He says this won't work, but doesn't give details or the actual code used, and I'm sure that it would be relatively easy to make this work. But you are correct, @Patrick, we need an example data set in the form of SAS data step code so we can work with this.

 
--
Paige Miller
PaigeMiller
Diamond | Level 26

Loop what date values?

 

Loop where in the code?

--
Paige Miller

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 713 views
  • 0 likes
  • 4 in conversation