Catherine Papanikos
Catherine Papanikos

Reputation: 11

Why put function doesn't work well in sas

I want to have a sas dataset with 1 decimal of some variables, so my code is the following

data a;
    set a;
    dif=put(t0d,4.1);
    drop t0d;
run;

Although in some cases with the dif variable I don't have this format. For example I have

dif 
-1.0
-9 
15.0
2 
3.0 
5.0 
15.0

how can i fix this ?? I want

dif 
-1.0
-9.0
15.0
2.0
3.0 
5.0 
15.0

Thank you!!

Upvotes: 0

Views: 615

Answers (2)

Negdo
Negdo

Reputation: 532

Put function DOES work well in SAS, you just have to use it correctly.

As for what you want to do, it depends on what kind of variable is your t0d. If it is char, then you have to use INPUT function:

DATA have;
input x $; 
datalines;
8722
-93.2
-0.1122
15.116
5
1.5
;
run;

data want;
    set have;
    dif=input(x, 8.);
    drop x;
    format dif 8.1;
run;

But if the original variable is numeric, you just put a proper format on it. You can put it where you created the original table, or just use format statement.

DATA have;
input x ; 
datalines;
8722
-93.2
-0.1122
15.116
5
1.5
;
run;
 
data want;
    set have;
    format x 8.1;
run;

Upvotes: 0

Tom
Tom

Reputation: 51611

I suspect you have left something out of your explanation. The code you showed works fine for the values you showed.

data test;
  input t0d;
  dif = put(t0d,4.1);
cards;
-1.0
-9 
15.0
2 
3.0 
5.0 
15.0
;
proc print;
run;

Results (plain old text output)

Obs    t0d    dif

 1      -1    -1.0
 2      -9    -9.0
 3      15    15.0
 4       2     2.0
 5       3     3.0
 6       5     5.0
 7      15    15.0

As you can see the new variable DIF is character of length 4 with the strings right aligned.

If instead you wanted DIF to be a numeric variable then change the code to just assign the value and attach a format to DIF so that the default way that the values are displayed is as 4 character strings with one decimal place.

dif = t0d ;
format dif 4.1;

PS The ODS output system does not display the leading spaces.

enter image description here

Upvotes: 1

Related Questions