BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
spraynardz90
Calcite | Level 5

Hi

Looking to perform a similar function in SAS to =Left(x,4)  on excel.

Does anyone know the command for this?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you really have these variables as character strings then SUBSTR(x,1,4) will take the first 4 characters of the field.  If for some reason the value is pushed right in the field you could use the LEFT() or STRIP() function to remove the leading spaces first.

More likely you have the data stored as a number.  If it is really a date in the format YYYYMMDD then you should store it as a date.  Then the YEAR() function will be able to take the year part of the date for you.

If instead you just have it stored as number (For example 19,701,231) then you could use INT(datadate/10000) to find the digits from the 10 million to 10 thousands place.  Or you could convert it to a date and then use the YEAR function.  year(input(put(datadate,z8.),yymmdd8.))

View solution in original post

4 REPLIES 4
shivas
Pyrite | Level 9

Hi,

You can try using Substr function or directly you can use year function to extract the year from the given date.

cc=year(datadate);

Thanks,

Shiva

Tom
Super User Tom
Super User

If you really have these variables as character strings then SUBSTR(x,1,4) will take the first 4 characters of the field.  If for some reason the value is pushed right in the field you could use the LEFT() or STRIP() function to remove the leading spaces first.

More likely you have the data stored as a number.  If it is really a date in the format YYYYMMDD then you should store it as a date.  Then the YEAR() function will be able to take the year part of the date for you.

If instead you just have it stored as number (For example 19,701,231) then you could use INT(datadate/10000) to find the digits from the 10 million to 10 thousands place.  Or you could convert it to a date and then use the YEAR function.  year(input(put(datadate,z8.),yymmdd8.))

C_Ishihara
Calcite | Level 5

This is exactly what I needed!  Thanks!

For those using Miner, I have an account number 15 digits long (Char15). I only need the first 11.

SAS Code Node read:

data &EM_EXPORT_TRAIN; set &EM_IMPORT_DATA;

CM11=SUBSTR(ACCT_NO,1,11);

run;

Returns the same value as Excel; =

Left(ACCT_NO,11)

Astounding
PROC Star

If DATADATE is character, and left-hand-justified, here are a couple of simple tricks to getting the year.

If you want YEAR as numeric, read just the first four characters of DATADATE:

year = input(datadate, 4.);

If you want YEAR as character, copy into a shorter variable:

length year $ 4;

year = datadate;

If DATADATE is numeric, then other posters have mentioned the right issues.  You have to begin with asking what number is actually stored there (the digits in your post, vs. a SAS date).

Good luck.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 39704 views
  • 0 likes
  • 5 in conversation