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!

CODE does not match exactly 2

Status
Not open for further replies.

DonaZ

Technical User
Oct 10, 2007
41
US
This question is in reference to
thread376-1455435 which is now a closed thread.

This code worked before
Data TABLEA;
set Table2;
hcode = put(input(hcode,6.),z6.);
lcode= put(input(lcode,6.),z6.);
End;
run;

However, now the code is a numeric field. I thought I followed instructions from Chris but apparently, I did not follow the instructions correctly. I took the INPUT part out, but it failed. What did I do wrong?

data TABLEA;
set Table2;
hcode = put(hcode,6.),z6.);
lcode= put(lscode,6.),z6.);
End;
run;


Thank you.

DonaZ
 
Hi DonaZ,

Both put and input functions require only 2 parameters - the put function does have a third parameter but that is to handle justification.

If hcode is now numeric, then you don't need the input function as that was to convert it to numeric so you could use the z. format which is a numeric format.

Try this:

Code:
hcode = put(hcode,z6.); 
lcode = put(lscode,z6.);

Alternatively:
Code:
format hcode lcode z6.;

HTH
 
Also note you can't change the TYPE of the variable, you need to load the data into a NEW variable.
When doing this sort of thing, I find it less confusing if I suffix the variable names with _TX for text fields and _NM for numbers.
It's also useful to specify the lengths of the text variables before loading data into them, in this case it shouldn't matter as you're loading a 6 digit padded field into it, so they'll default to 6 bytes. However, in other situations where what you're loading into the field is of a variable length, the variable will get the length of the first value loaded into it, which can lead to truncation in later values.
Code:
  length hcode_tx lcode_tx  $6;
  hcode_tx = put(hcode_nm,z6.); 
  lcode_tx = put(lscode_nm,z6.);


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
I'm sorry, my query is not working. What am I doing wrong?


I meant to say that the field is currently a numeric that needs to be converted to a text. The field needs to accommodate the leading zeros.

 
hi

whenever I need to do this (numerical to caharcter I use

var1 = input(put(var2, 8.2), $8.);

if you start of with a numerical field, then I doubt you will be able to keep with the zeros ay the beginning, but if you need them try

var1 = input(put(var2, 8.2), $8.);
var1= left(reverse(var1));
var2 = substr(compress(var1,)||'0000000'),1,8);
newvar= left(reverse(var2));

hopefully this should do it.

cheers

Alun
 
Donaz.
IT's important to know what you're trying to achieve, and what you've got to work with.
The INPUT function works similarly to the INPUT statement, which reads data from a TEXT file into SAS. If you think of it like that it's easier to remember. The INPUT function is used to read in formatted text data, and put the results into a new field. To do this you supply it with the field that holds the TEXT string, then tell it what format it is currently in. For instance:-
Code:
data _null_;
  date = '12/06/2007';
  sasdate = input(date,ddmmyy10.);
  put date=;
  put sasdate=;
run;
Run the above to see how it works. This makes it very useful for writing a number stored in a text field into a new numeric field.

The PUT function is used to apply a format to a field, either text or numeric, and write the output to a new text field. Witht he put function, you tell the function what format you want the RESULT to be in.
Code:
data _null_;
  sasdate = '09JUN2007'd; *results in a SAS date*;
  date = put(sasdate,ddmmyy10.);
  put sasdate=;
  put date=;
run;
So those are your tools.
The next thing you need to do is look at your data, either using proc contents, or right click on the dataset and view the columns. Look at what data type the fields are, what the data actually looks like (what formats, if any, have been applied) and how you're going to convert it from what it is to what you want it to be.
Note - If the field has a format on it already, you'll want to know what the underlying data is like (if it's a date format on a numeric field for instance you need to understand that you have a number which is being presented to you formatted as a date, but that when you work with it, it is still just a number).
I hope that this helps, I think it's a pretty comprehensive coverage of PUT and INPUT. It's a tricky topic and one I've only really got to grips with over the last couple of years.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Thank you alunbrain and Chris for your response.
Have a good weekend.

DonaZ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top