BookmarkSubscribeRSS Feed
spg
Obsidian | Level 7 spg
Obsidian | Level 7
Hi,

I have a date format, e.g. 06/01/1989 and I want a new variable with just the year in it…1989 in this case. However, when I use a substr function, it is returning me odd values.
data one;
set two;
NewVariable=susbtr(date,7,4);
run;

I checked the column description for the date variable and it says
TYP:NUM
LENGTH: 8
FORMAT MMDDYY10.
INFORMAT MMDDYY10.

Where am I going wrong?
5 REPLIES 5
deleted_user
Not applicable
You should use the year function
spg
Obsidian | Level 7 spg
Obsidian | Level 7
that was easy! thanks much melc!
Cynthia_sas
SAS Super FREQ
Hi:
The SUBSTR function is meant to work with character strings or character variables. Therefore, using it with a numeric variable would have given you unpredictable results. Numbers are stored in such as way as to make it impossible to go to position 7 or position 4 of the number -- only character strings have fixed positions. So in the variable ALPHA, a character variable of 26 characters:
[pre]
ABCDEFGHIJKLMNOPQRSTUVWXYZ
[/pre]

This code:[pre]
newvar = substr(alpha,7,1);
[/pre]

would "substring" or extract the 7th character or the G from the alpha variable value.

A SAS numeric variable that represents a date is NOT stored in a "readable" format. For example, the date 01/01/1960 is internally stored as the number 0; while 12/31/1959 is stored as -1 and 01/02/1960 is internally stored as the number 1. This information is explained in the documentation here:
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001304321.htm
and
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001397898.htm

cynthia
SASPhile
Quartz | Level 8
It worked fine for me.it is spelled wrong in your code susbtr instead of substr.

data one;
date='06/01/1989';
NewVariable=substr(date,7,4);
run;
Cynthia_sas
SAS Super FREQ
Hi:
If date is a character string (such as you show), then SUBSTR will work. But if date is a numeric value, as shown in the program below, then the results will not be correct. Since the OP said his date value was numeric, SUBSTR on a numeric variable would force a conversion of the internally stored number from numeric to character in order for the SUBSTR to work (since it will ONLY work on CHARACTER variables or text strings).

See the log and program below, where the internally stored date for 06/01/1989 is the number 10744 (Jun 1, 1989 is 10744 days from Jan 1, 1960). So when 10744 was converted to a text string it was converted with the BEST12. format, which resulted in the string xxxxxxx10744 (where every x is a space). So the SUBSTR would get x107 -- where the x is a space -- so the substr results in newvar2 having the string ' 107' with a leading space. (The PUT statement does not show leading spaces -- nor do most SAS procedures.)

cynthia
[pre]
1289 data one;
1290 chardate='06/01/1989';
1291 NewVariable=substr(chardate,7,4);
1292 put chardate= newvariable=;
1293
1294 numdate = '01jun1989'd;
1295 newvar2 = substr(numdate,7,4);
1296 put "formatted val for " numdate= mmddyy10. " Internal val for " numdate= newvar2=;
1297 run;

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
1295:18
chardate=06/01/1989 NewVariable=1989
formatted val for numdate=06/01/1989 Internal val for numdate=10744 newvar2=107
NOTE: The data set WORK.ONE has 1 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds

[/pre]

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1121 views
  • 0 likes
  • 4 in conversation