DATA Step, Macro, Functions and more

Formatting Data

Accepted Solution Solved
Reply
Regular Contributor
Posts: 215
Accepted Solution

Formatting Data

Hi there,

I have a 11 caracter (Length 11, Format $11., Informat $11.) long text which is  '83.0000000'.

I want to convert it as 083 (character type). Can someone help please.

 

83.0000000 should be 083

95.0000000 should be 095

100.000000 should be 100

 

I wrote the following code, but it still doesn't help.

 

DATA Have;
A='83.00000000';
RUN;

DATA Out;
SET Have;
A=INPUT(A,z3.);
RUN;

 

Thanks,


Accepted Solutions
Solution
‎05-20-2016 05:02 PM
Frequent Contributor
Posts: 102

Re: Formatting Data

Don't know why the length would matter but here it is:

 

data out; length a 3.;
 set have(rename=(a=OldA)); 
 a=input(OldA,3.);
 format a z3.;
 drop OldA;
run;

 

I re-wrote it a bit to preserve your variable name A using RENAME in the SET statement.

View solution in original post


All Replies
Super User
Posts: 10,483

Re: Formatting Data

A = substr(a,1,index(a,'.')-1);

Regular Contributor
Posts: 215

Re: Formatting Data

Hi ballardw,
Your code returns 83, 93, 100. and the length is still 11, but I want it like 083, 093, 100. Thanks,
Frequent Contributor
Posts: 102

Re: Formatting Data

You cannot change the format of the variable in DATA step.  Your variable is text in both HAVE and OUT.  Also, z3. is not useful as an informat.  Try this:

 

data have;
a='83.00000000';
run;
data out;
set have;
b=input(a,3.);
format b z3.;
run;

Regular Contributor
Posts: 215

Re: Formatting Data

Thanks Haris,
It works, but the length is still 8, how can i minimize it to 3?
Solution
‎05-20-2016 05:02 PM
Frequent Contributor
Posts: 102

Re: Formatting Data

Don't know why the length would matter but here it is:

 

data out; length a 3.;
 set have(rename=(a=OldA)); 
 a=input(OldA,3.);
 format a z3.;
 drop OldA;
run;

 

I re-wrote it a bit to preserve your variable name A using RENAME in the SET statement.

Super User
Super User
Posts: 6,498

Re: Formatting Data

SAS always stores numbers as floating point.  So you can reduce the storage from 8 to 3 as long as you only have small integers.

But is sounds like you want to convert your 11 character string to a 3 character string.  You can use a number as an intermediate step.

So if your original variable is named HAVE this will create a new variable named WANT that is length $3 and has the leading zeros.

 

want = put( input(have,11.) , Z3.) ;

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 352 views
  • 1 like
  • 4 in conversation