Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how come length is 7 and answer is under 6

Status
Not open for further replies.

bbprakash

Programmer
May 8, 2008
6
GB
hai,
I am a starter in SAS and while I was doing the following progrmme I found answer as 'length 7' and 'under 6'. I want explenation how it does.
data fltaten;
input jobcode $ salary name $;
cards;
FLAT1 70000 Bob
FLAT2 60000 Joe
FLAT3 30000 Ann
;
run;
data desc;
set fltaten;
if salary>60000 then description='Over 60';
else description='Under 60';
run;

bhanu
 
You need to add in a length statement to set the length of the "description" variable. Otherwise SAS gives it the length of the first value loaded into it, so your "Over 60" sets the length as 7.
Code:
data desc;
   set fltaten;
   
   length description $10;
   
   if salary>60000 then description='Over 60';
   else description='Under 60';
run;
That should sort you out.
I would also recommend doing the same in the first datastep for each of the values there to avoid similar truncation issues. It is good practice to ALWAYS do this when reading in data.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Thank you Mr. Chris.
Could you please tell me the use of substr function for numerical values is it same as for charecters. Here is example
data work.AreaCodes;
Phonenumber=3125551212;
Code='('!!substr(Phonenumber,1,3)!!')';
run;

answer is ( 3).
 
No, you can't use substr with numeric data. If you run the code you included above, you'll get a message like this:-
NOTE: Numeric values have been converted to character values at the places given by:

What you should do is convert the data to text.
Also, for things like telephone numbers, you should use a text field anyway, not a numeric. Numeric fields only have a maximum of 8bytes to store the number. Once the number gets above a certain magnitude, it starts suffering from rounding issues. For actual numeric data this possibly isn't such a big deal, however a telephone number that has been rounded to the nearest 3 is totally useless. This also applies to account numbers, credit card numbers, and in fact, I'd say ANY number which is a code rather than a value should be stored as a character field.
I have hit upon this issue before, and it's difficult to track down.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top