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

Hi all SAS Users,

Theoretically speaking, SAS attempts to convert from numeric to the character in the character expression (like scan, catx...) or convert from character to numeric in arithmetic expression( like sum, mean,..). And I read a note from my course, it documents that: "Sometimes it works just fine, and other times you can get the unexpected results."

 

To me, yes, when converting from character to number, sometimes it does not work properly because of the comma in the character can confuse SAS so SAS will generate a missing variable. But when converting number to char, I cannot find any case that converting from numeric to var can cause the error. Can you please help me to point it out.

Furthermore, even the code to explicitly convert from char to number and number to char seems to support my idea as well:

Phil_NZ_0-1617513388207.png

As can be seen, to convert from the character to number, we must use input with informat (for altering input purpose), but when convert from number to character to number, we only need format (for displaying purpose), so how come automatically converting from number to character causing the error.

 

Can you please help me to sort it out?

 

Warm regards and thanks.

 

P/S: Another novice question: Can you please tell me what function can alter the "minus" and can ignore the missing value?

for example: rather than x1-x2, what function we can use. The same pattern that rather than x1+x2, we can use function sum(x1,x2).

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Not an "error" but the format used to convert from numeric to text is a "best" format unless you specify a different one.

One example of "unexpected results":

data junk;
   x= 1.23;
   y= scan(x,1);
run;

Quick, before running the code what do you expect the result of y to be in the above? Now check.

And this one:

data junk;
   x= 1234567891123456;
   y= scan(x,1);
run;

Part of the issue above is SAS  "converts" then applies the Scan function and decimals are delimiters in Scan by default.

Other functions may have other peculiarities. Such as:

data junk;
   x= 1234567.891123456;
   y= catx('_','ABC', x);
run;

Integer numeric values are unlikely to have many problems for most conversions. Values with decimal components are extremely iffy as to getting the desired result you may want.

 

I am not sure exactly what you are getting with at the the second question. Perhaps sum(x1, -1*x2)?

Subtraction is not nice and you have to code around it. By not nice I mean it is none of reflexive, symmetric or transitive unlike addition.

 

 

View solution in original post

2 REPLIES 2
ballardw
Super User

Not an "error" but the format used to convert from numeric to text is a "best" format unless you specify a different one.

One example of "unexpected results":

data junk;
   x= 1.23;
   y= scan(x,1);
run;

Quick, before running the code what do you expect the result of y to be in the above? Now check.

And this one:

data junk;
   x= 1234567891123456;
   y= scan(x,1);
run;

Part of the issue above is SAS  "converts" then applies the Scan function and decimals are delimiters in Scan by default.

Other functions may have other peculiarities. Such as:

data junk;
   x= 1234567.891123456;
   y= catx('_','ABC', x);
run;

Integer numeric values are unlikely to have many problems for most conversions. Values with decimal components are extremely iffy as to getting the desired result you may want.

 

I am not sure exactly what you are getting with at the the second question. Perhaps sum(x1, -1*x2)?

Subtraction is not nice and you have to code around it. By not nice I mean it is none of reflexive, symmetric or transitive unlike addition.

 

 

Tom
Super User Tom
Super User

P/S: Another novice question: Can you please tell me what function can alter the "minus" and can ignore the missing value?

for example: rather than x1-x2, what function we can use. The same pattern that rather than x1+x2, we can use function sum(x1,x2).

The easiest is sum(x1,-x2) which will not generate notes when X1 is missing.  But it will when X2 is missing.  But you can use the fact that 0 is the additive identity eliminate errors when X2 is missing.

sum(x1,-coalesce(x2,0))

But note that this will result in 0 when both X1 and X2 are missing.

You could use IF/THEN logic instead to explicitly tell it what to do in the case of missing values.

if missing(x2) then newvar=x1;
else if missing(x1) then newvar=-x2;
else newvar=x1-x2;

Note if you need to explicitly set NEWVAR to missing when both X1 and X2 are missing then you will need to test for that condition also.  

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 1318 views
  • 2 likes
  • 3 in conversation