BookmarkSubscribeRSS Feed
navyaroopa
Calcite | Level 5

Hi all,

 

I wanted a format for displaying year when a variable year is in 2 digits and options yearcutoff is used: 

I have done a programing like below mentioned:

 

data v;
input v $4.;
datalines;
17
20
14
1
;
run;

 

option yearcutoff=2000;
data v1(drop=catv);
set v;
if length(v)=2 then do;
catv= input("01/01/"||strip(v),ddmmyy8.);
date=year(catv);
end;
end;

run;

 

I dont want this big code. Is there any format where i can use and can get my year value in 4 digits. Could someone help me with this.

 

Many Thanks

 

 

 

6 REPLIES 6
Kurt_Bremser
Super User

Example:

data v;
input v $4.;
date = '2000';
substr(date,4-length(strip(v))+1) = strip(v);
datalines;
17
20
14
1
;
run;

proc print data=v noobs;
run;

Result:

v     date

17    2017
20    2020
14    2014
1     2001
navyaroopa
Calcite | Level 5

Hi, 

 I am vry sorry, i have forgot to mention this , I dont want any value to be hardcoded (date='2000'). I am trying to automate the program.

 

Many Thanks.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry, how are you planning on automating a process without all the required information?  A year is a four digit number YYYY, if you only have two digits YY, then you will need somehow to specify to the program what the default other two numbers are, is 17 = 1917, 1817, 1717, or 2017?  That is information you have to provide as there is now other logical way of knowing what you think those two should be.

Kurt_Bremser
Super User

@navyaroopa wrote:

Hi, 

 I am vry sorry, i have forgot to mention this , I dont want any value to be hardcoded (date='2000'). I am trying to automate the program.

 

Many Thanks.


You hardcoded your year with

option yearcutoff=2000;

in your initial post. What needs to be automated?

Tom
Super User Tom
Super User

So if you know that your YR2 variable is between 0 and 99 then you can just use something like this.

yr4 = year(input(put(yr2,z2.)||'0101',yymmdd6.));

Or were you looking to find an algorithm to implement the year cut off functionality yourself with using any informats?

%let yearcutoff=%sysfunc(getoption(yearcutoff));
data want ;
  input yr2 @@ ;
  yr4 = 100*((yr2<mod(&yearcutoff,100))+int(&yearcutoff/100))+yr2;
  yr4_date = year(input(put(yr2,z2.)||'0101',yymmdd6.));
  put (_all_) (=);
cards;
1 20 25 26 27 30
;

 

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!
Discussion stats
  • 6 replies
  • 1519 views
  • 0 likes
  • 4 in conversation