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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change a variable?

Status
Not open for further replies.

njon315

MIS
Oct 12, 2008
5
US
It's OK to change character to numeric variable:

data new;
set old ;
income = input(income comma6.2);
run;

Is there any way to change within the existing data set?
Thanks in advance.

p.s. how do I reply a thread?
 
Hi njon,

No, you can't store character data in a numeric variable. Basically there are only two data types in SAS: Numeric and Character. The way memory allocation is handled for each one is very different: Characters are quite straightforwardly stored using their ASCII code allocating a byte for each character; all numeric data is stored as floating point representation and is made up of a sign, base, mantissa and exponent. This page give a rather in-depth description of all this.


If your variable "income" contains a character and you want to convert it to numeric then assign it to a new variable.

Code:
nvar = input(income,comma6.2);

If you are looking to apply the comma6.2 format on the numeric variable income then use a format statement.

Code:
format income comma6.2;

HTH
 
to do this njon you can code it like this:-
Code:
data old;
  set old(rename=(income=income_tx));

  length income 8.;

  income=input(income_tx,comma6.2);
run;

However, this is bad practice.
What you SHOULD do is fix up the code that created the INCOME field in the first place so that the variable starts as a numeric.
If you're reading it from a text file for instance:-
Code:
data old;
  infile blah recfm=v lrecl=256 dsd dlm='09'x missover firstobs=2;

  length income 6;

  input income;
run;
is a much better way of doing it.

The reason the first method is bad practice is that the code cannot be re-run a second time without re-creating the OLD dataset.


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Thank you very much for your reply. This really helps me a lot.
 
I have a question.
When I input variables and define the format. SAS always puts characters variables together by changing th column order. Such as:
f1 f18 f19 f2 f3
A0N1 77 F/ 65 % 2% 1 0.127
A0N1 77 F/ 65 % 2% 1 0.128
A0N1 77 F/ 65 % 2% 1 0.128
A0N1 77 F/ 65 % 2% 1 0.126
A0N1 77 F/ 65 % 2% 2 0.128
A0N1 77 F/ 65 % 2% 2 0.128
A0N1 77 F/ 65 % 2% 2 0.129

How to fix it?
 
Hi,

The order of variables in a dataset is determined by the order they are first presented to the compiler. This can be influenced by compile time statements such as length, attrib, retain and format. One way to get round this is to define formats after the input statement.

Code:
data test;
input f1$ f2 f3 f4$;
format f1 f4 $3.;
cards;
test 1.1 2.2 test
test 3.3 4.4 test
;run;
proc print;run;

For length statements, as this has to be defined before the compiler sees the input, then specify the length statements in the order you want them to be displayed for all variables.

Code:
data test;
length f1 $3 f2 f3 8. f4 $3;
input f1$ f2 f3 f4$;
cards;
test 1.1 2.2 test
test 3.3 4.4 test
;run;
proc print;run;

HTH

proc print;run;

 
kdt82 - The format statement is a "non-executable" statement, so it is in fact run before everything else in the step.
For example:-
Code:
data _null_;
  date = '01JAN2007'd;
  put date=;
  format date ddmmyy10.;
run;
you'd expect this code to give you a number as output, but you actually get a formatted date.
SAS does some weird things sometimes. :)

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

Part and Inventory Search

Sponsor

Back
Top