BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mlogan
Lapis Lazuli | Level 10

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Haris
Lapis Lazuli | Level 10

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

6 REPLIES 6
ballardw
Super User

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

mlogan
Lapis Lazuli | Level 10
Hi ballardw,
Your code returns 83, 93, 100. and the length is still 11, but I want it like 083, 093, 100. Thanks,
Haris
Lapis Lazuli | Level 10

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;

mlogan
Lapis Lazuli | Level 10
Thanks Haris,
It works, but the length is still 8, how can i minimize it to 3?
Haris
Lapis Lazuli | Level 10

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.

Tom
Super User Tom
Super User

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.) ;

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1333 views
  • 1 like
  • 4 in conversation