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

Hi RichardInOz,

Sorry; going to have to reply to your post.  As far as I am aware there is no method of performing calculations on a character variable.  The data numeric vs character is very different at a binary level.   Yes there are ,any clever solutions which may supress warnings, however behind the scenes they are all converting the character to numeric, then performing the calculation, then converting back again.  There is no way round it, characters need to be converted to numeric for mathmatical operations to be performed. Wether you are happy to let the machine decide when to do this is of course, up to you.  For my money I would want to see it, whats a simple input statement in the great scheme of things.

RichardinOz
Quartz | Level 8

Wot???

The formula is entirely numeric, intended to operate on a numeric variable, as described by the OP.

As it happens I am in agreement with you about automatic conversions.

Apology accepted

Richard

mmabika
Calcite | Level 5

Hi !

According to the documentation send by , we can read that:

When Variable Length Equals ... 

Largest Integer

z/OS

Largest Integer

Windows/UNIX

2

256

not applicable

3

65,536 

8,192

4

16,777,216

2,097,152

5

4,294,967,296

536,870,912

6

1,099,511,627,776

137,438,953,472

7

281,474,946,710,656

35,184,372,088,832

8 (default)

72,057,594,037,927,936

9,007,199,254,740,992

When viewing this table, consider the following points:

The minimum length for a SAS variable on Windows and UNIX operating systems is 3 bytes, and the maximum length is 8 bytes

But we know that the lenght of a numeric variable affects only the output of that variable. So to extract the first digits using substr() function, you have to put the beginning position to 1 and the end position (count from the first digit to the last one to conder where the first digit represent 3 or 4 according to the numeric to treat)

Exemples:

data want;

a = 123456789;

phonenum = 3125551212 ;

b=substr(a,1,7);  /* to get 7, the first digit begin at 4*/

c=substr(a,1,11);

mim=substr(phonenum,1,12); /*to get 12, the first digit begin at 3 */

run;

proc print data=want;

run;

numsas.png

LinusH
Tourmaline | Level 20

Sounds you are mixing data storage rules (the table are visualizing no of numerical bytes and integer accuracy).

To do sub-strings, you are probably in to look at numerical values as humans see them, in it's decimal form.

Sometime SAS is nice to us and let us do string operations on numerical values, but best practice is to put a numerical value using a format, before attempt to use any string operations. This is similar to CAST in other SQL based DBMS.

Whether your examples work or not depends on the length of the char variables b, c and mim - quite risky. Risks should be minimized in your programming.

Data never sleeps
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I quite agree with you Linus Hjorth.  My point above was simple:

String data (array of characters) - needs to be converted to numeric for calculations to be done.

Numeric data - needs to be convert to a string (array of characters) for textual manipulations.

Sure, SAS will be helpful and behind the scenes convert the data for you in some events, but as you say I would view this as very risky.  It would to my mind be sloppy programming to not explicitly convert data when necessary to do certain things with which the data type is not associated.

I also note that this topic is 4 months old now Smiley Happy

LinusH
Tourmaline | Level 20

String and numerical conversion has been one of the top stories since the dawn of SAS, so I guess this is a die hard topic...:smileysilly:

Data never sleeps
jakarman
Barite | Level 11

Would not go into any details as there are 10 persons understanding the digital world.
https://support.sas.com/techsup/technote/ts654.pdf

Numerics floating and the digital representation? Take also notice of the precision.

---->-- ja karman --<-----
jakarman
Barite | Level 11

RichardinOz, Not all questions of some OP are making some sense. Trying to give technical answer will harm them more in the end. To which of the 10 persons do you belong?

Op's question wqas about the first few digits of the numeric. Please solve that in Roman notation: SAS(R) 9.4 Formats and Informats: Reference. the question is humbug. 

---->-- ja karman --<-----

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 22 replies
  • 159861 views
  • 15 likes
  • 10 in conversation