Using variables in a conditional statement?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Using variables in a conditional statement?

I am new to SAS.  I have been able to read a file, select records and print a simple report. The task I have is to select and report on all records from an external file that have last years date. Is there a method of extracting the CCYY portion of the date from the current system date into a variable that I can use in a conditional statement when I am selecting records? This would allow me to run this report going forward without having to hard code my confitionals.

The book I have to work with does not address anything like this.

Thanks


Accepted Solutions
Solution
‎02-12-2014 07:34 PM
Super User
Super User
Posts: 6,502

Re: Using variables in a conditional statement?

So if the current year is 2014 you want to generate comparisons that look like:


(20130000 < DATCLOS < 20140000)

Rather than creating variables in the data set, just use macro variables to generate the string of digits you want to use.  So put these two lines at the top of your program or just before your data step where you are going to need to reference this year and last year.  They will create two macro variables that will hold 2014 and 2013.

%let thisyear = %sysfunc(today(),year4.);

%let lastyear= %eval(&thisyear - 1);

Then in your IF statement you can include this test:

(&lastyear.0000 < DATCLOS < &thisyear.0000)

View solution in original post


All Replies
Super User
Posts: 5,257

Re: Using variables in a conditional statement?

    Yes, the automatic macro variable SysDate hold this information. Use %substr to get the CCYY part.

Data never sleeps
Occasional Contributor
Posts: 8

Re: Using variables in a conditional statement?

I am obviously not getting it. Heres what I tried, it returned no observations and I tried to include the variables in the output file to see what values were in there and when I looked at it they where empty

DATA  EXTRACT1 ;   INFILE LOANEXT  ;           

TESTDT1= SUBSTR(SYSDATE9,6,4) ;                

TESTDT2= SUBSTR(SYSDATE9,6,4) ;                

* TESTDT1= TESTDT1 - 1 ;                       

* TESTDT1= TESTDT2 * 10000 ;                   

* TESTDT2= TESTDT1 * 10000 ;                   

                                                

INPUT                                          

  @    1 CLIENT     3.

000036  @    4 APP        2.                                           

000037  @    6 LN        13.                                           

000038  @   85 OGLDSBDT  PD5.                                          

000039  @  117 ACTSTA    1.                                            

000040  @  120 DATCLOS   PD5.                                          

000041  @  143 LNTYP     PD2.                                          

000042  @  154 LNCLSCOD  1.                                            

000043  @  547 PYFCOFCD  $2.                                           

000044  @ 2428 PRICUSNM  $30.                                          

000045  @ 2488 PRIADDR1  $30.                                          

000046  @ 2518 PRIADDR2  $30.                                          

000047  @ 2548 PRICITY   $18.                                          

000048  @ 2566 PRISTATE  $3.                                          

000049  @ 2569 PRIZIP    $9. ;                                 

000050                                                         

000051 IF    (ACTSTA = '3' OR '4')                             

000052 AND   PYFCOFCD IN ('IR', 'VR', 'CR', 'UR')              

000053 AND   DATCLOS > TESTDT1        * / looking for 20130000                         

000054 AND   DATCLOS < TESTDT2        * /looking for 20140000                         

000055 AND   LNCLSCOD = 4 ;                                    

000056                                                         

000057                                                        

.

.

.

000073 DATA  _NULL_   ;                                 

000074 TESTDT1= SUBSTR(SYSDATE9,6,4) ;                  

000075 TESTDT2= SUBSTR(SYSDATE9,6,4) ;                  

000076 * TESTDT1= TESTDT1 - 1 ;                         

000077 * TESTDT1= TESTDT1 * 10000 ;                     

000078 * TESTDT2= TESTDT2 * 10000 ;                     

000079  SET  WORK1     ;                                

000080   FILE  OUTFILE1 ;                               

000081                                                  

000082   PUT                                            

000083    @    1 CLIENT        Z3.                      

000084    @    5 LNTYP         Z3.                      

000085    @    9 PYFCOFCD      $2.                      

000086    @   12 OGLDSBDT      Z8.                      

000087    @   22 BK            Z3.                      

000088    @   26 APP           Z2.  

000089    @   29 LN            Z13.                                           

000090    @   43 TESTDT1       8.                                             

000091    @   52 TESTDT2       8. ;                                           

000092 * /@   43 PRICUSNM      $30.                                           

000093 * /@   75 DATCLOS       Z8.  ;                                         

000094                                                                        

000095 * THE REPORT SECTION IS HERE ;                                         

000096                                                                        

000097                                                                        

000098 TITLE  '1099A' DATE= &SYSDATE9;                                        

000099 TITLE2 '                      xxxxxxxxxx FINANCIAL CORP' ;             

000100 TITLE3 '                BUSINESS ACCOUNTS REPOSSESSED THIS YEAR' ;    

********************************* Top of Data **********************************

001 009 VR 20080205    . 01 0000003087691                                      

001 009 VR 20080620    . 01 0000004139246                                      

001 009 VR 20080925    . 01 0000004167744                                      

001 009 IR 20081110    . 01 0000004177530                                      

001 009 VR 20081119    . 01 0000042181757                                      

001 009 IR 20100302    . 01 0000004295569                                      

001 009 IR 20100616    . 01 0000004336838                                      

001 009 VR 20100805    . 01 0000004353370                                     

Super User
Posts: 17,864

Re: Using variables in a conditional statement?

The automatic variable means its a macro variable, ie &sysdate

You can use the intnx function to increment the date and then use a format to get to CCYY portion, except I don't know what CCYY stands for Smiley Happy

Occasional Contributor
Posts: 8

Re: Using variables in a conditional statement?

century and year

what would the syntax look like for your suggestion. I apologise , I am very new to this and am learning from a book

I want to create a variable I can use in my 'if' statement when reading a file. The variables will be CCYYMMDD with the MMDD portions containing zeros so that the 'if' statement will be 'IF DATCLOS > TESTDT1  (EG 20130000)  AND DATCLOS < TESTDT2 (20140000) i was wanting to create the variables from the systemn date and obviousely don't know how.

Regards

Solution
‎02-12-2014 07:34 PM
Super User
Super User
Posts: 6,502

Re: Using variables in a conditional statement?

So if the current year is 2014 you want to generate comparisons that look like:


(20130000 < DATCLOS < 20140000)

Rather than creating variables in the data set, just use macro variables to generate the string of digits you want to use.  So put these two lines at the top of your program or just before your data step where you are going to need to reference this year and last year.  They will create two macro variables that will hold 2014 and 2013.

%let thisyear = %sysfunc(today(),year4.);

%let lastyear= %eval(&thisyear - 1);

Then in your IF statement you can include this test:

(&lastyear.0000 < DATCLOS < &thisyear.0000)

Super User
Posts: 17,864

Re: Using variables in a conditional statement?

Would there be any efficiency in importing the dates as "Dates" first?

If you're doing further analysis this seems like it would be a good idea.

Occasional Contributor
Posts: 8

Re: Using variables in a conditional statement?


This is perfect and worked a charm. Thank you Tom, I will make a point of lesrning more about the use of macro variables as I proceed.

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 678 views
  • 1 like
  • 4 in conversation