turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- =LEFT(X,4) EXCEL STYLE FUNCTION ON SAS

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-17-2012 12:38 AM

Hi

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

Does anyone know the command for this?

Accepted Solutions

Solution

10-03-2017
07:48 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to spraynardz90

06-18-2012 12:09 PM

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.))

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to spraynardz90

06-17-2012 06:35 AM

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

Solution

10-03-2017
07:48 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to spraynardz90

06-18-2012 12:09 PM

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.))

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-09-2015 01:28 PM

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)

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to spraynardz90

06-18-2012 12:39 PM

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.