Community talk about communities.sas.com. Meta!

format or function for year

Reply
Occasional Contributor
Posts: 5

format or function for year

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

 

 

 

Super User
Posts: 6,936

Re: format or function for year

date = '2000';
substr(date,4-length(strip(v))+1) = strip(v);
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 6,936

Re: format or function for year

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
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 5

Re: format or function for year

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.

Super User
Super User
Posts: 7,401

Re: format or function for year

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.

Super User
Posts: 6,936

Re: format or function for year


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?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 6,500

Re: format or function for year

[ Edited ]

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
;

 

Ask a Question
Discussion stats
  • 6 replies
  • 173 views
  • 0 likes
  • 4 in conversation