BookmarkSubscribeRSS Feed
rajeshalwayswel
Pyrite | Level 9
data a;
input l $10.;
cards;
111.0000
1.0
0.00
0.000
0
22.120
262.100
run;

required output
111
1.0
0.00
0.0000
0
22.120
262.1
6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

What is the logic here? l is a character value, correct? Do you want the desired result to be character too?

rajeshalwayswel
Pyrite | Level 9
l is a character value, desired results needs to be in character.I too don't the logic I have data like this to show as required one.
PeterClemmensen
Tourmaline | Level 20

If you don't know what problem to solve, neither do we 🙂

Kurt_Bremser
Super User

Code is built on a logical specification. Without a logical rule for getting from A to B, nobody can do anything.

 

There is no (for me) discernible pattern in the data you gave us.

 

Amir
PROC Star

Can you go back to the person who asked you to do this work and ask them, what are the rules that change "111.0000" to "111", "1.0" to "1.0" (same), ...,  "262.100" to "262.1"?

 

Does the rule change based on the input value?

 

Does the number of non-zero digits affect the rules?

 

etc.

 

Then you can share the rules here and someone should be able to help if you still have problems.

 

If you get the solution yourself then please share that here too.

 

Amir.

FreelanceReinh
Jade | Level 19

Hi @rajeshalwayswel,

 

It appears that you want to apply a conditional format, i.e., one that depends on certain rules. So, maybe the PUTN function can be applied. But, as the others have pointed out, you need to know those "certain rules."

 

Here are two examples using rather arbitrary rules both of which produce the desired values (in a character variable W in dataset WANT) for your sample data using the w.d format with a varying value of d and left-justified by the -l modifier.

 

  1. Values of d are pre-specified in an array:
    data want;
    array d[7] _temporary_ (0,1,2,4,0,3,1);
    set a;
    length w $10;
    w=putn(input(l,10.),cats('10.',d[_n_],'-l'));
    run;
  2. [Fun solution:] Values of d follow a random pattern (Poisson distribution with parameter 2):
    data want;
    set a;
    length w $10;
    w=putn(input(l,10.),cats('10.',ranpoi(1382551861,2),'-l'));
    run;

 

If you need the formatted value just for output (e.g. to a text file), the $VARYINGw. format might be useful.

 

Example (similar to no. 1 above, but now the lengths are pre-specified and the results are written to the log😞

data _null_;
array len[7] _temporary_ (3,3,4,6,1,6,5);
set a;
w=put(input(l,10.),10.4-l);
varlen=len[_n_];
put w $varying10. varlen;
run;

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 895 views
  • 0 likes
  • 5 in conversation