## How to format variable in certain form?

Solved
Super Contributor
Posts: 398

# How to format variable in certain form?

Good moring, everyone:

I have one SAS data set.  One variable is "Value", and it's Characteristic.   I would like to change it to numerical.  Also, I would like to change two digital decimals.  Some of my "Value" are list below.  I think it should be used input function, but how to format them?

1.  53.6

2.  100

3.  8

4.  45238962

Thanks.

Accepted Solutions
Solution
‎04-05-2016 12:35 PM
Posts: 3,852

## Re: How to format variable in certain form?

I posted how to do basically that.

``````data value;
input value:\$16. type :\$1.;
length formatted_value \$16;
select(type);
when('O','C','U') formatted_value = putn(input(value,f16.),'F',16,2);
when('N','D')     formatted_value = value;
otherwise;
end;
cards;
53.6 O
100 O
8 O
45238962 N
;;;;
run;
proc print;
run;``````

All Replies
Super Contributor
Posts: 398

## Re: How to format variable in certain form?

I forget to say, Row 1-3 are two decimal. But row 4 will be the same, no decimal.

Posts: 3,852

## Re: How to format variable in certain form?

``````data value;
input value:\$16.;
d = ifn(_n_ in(1:3),2,0);
length formatted_value \$16;
formatted_value = putn(input(value,f16.),'F',16,d);
cards;
53.6
100
8
45238962
;;;;
run;
proc print;
run;``````

Super User
Posts: 10,209

## Re: How to format variable in certain form?

do

``new_value = input(value,W.);``

where W corresponds to the length of value; if you want to replace value, do

``````drop value;
rename new_value = value;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super Contributor
Posts: 398

## Re: How to format variable in certain form?

I have 454 observations in "Values", the format I list are scattered in this "Value" variable.

Posts: 3,852

## Re: How to format variable in certain form?

You need a way communicate the FORMAT for each line.  Show more data with IDs for the rows.

Super Contributor
Posts: 398

## Re: How to format variable in certain form?

The numbers from row 1-3 are corrospored to "O" of variable "Type", the number from row 4 are corrospored to "N" of variable "Type".

Posts: 3,852

## Re: How to format variable in certain form?

``````data value;
input value:\$16. type :\$1.;
d = ifn(type eq 'O',2,0);
length formatted_value \$16;
formatted_value = putn(input(value,f16.),'F',16,d);
cards;
53.6 O
100 O
8 O
45238962 N
;;;;
run;
proc print;
run;``````
Super Contributor
Posts: 398

## Re: How to format variable in certain form?

Hi:

Could you explain to me what is the mean for f16. and 'F' standing for?   I'm lost for the code of       when('O','C','U') formatted_value = putn(input(value,f16.),'F',16,2); ?

Thanks.

Super Contributor
Posts: 398

## Re: How to format variable in certain form?

If my data is list below, how could I go from there?  I would like to keep two decimal for type "O", "C" and "U", and keep the same for type "N" and "D".

`53.6 O 100 C 8 U 45238962 N 618945 D`

Solution
‎04-05-2016 12:35 PM
Posts: 3,852

## Re: How to format variable in certain form?

I posted how to do basically that.

``````data value;
input value:\$16. type :\$1.;
length formatted_value \$16;
select(type);
when('O','C','U') formatted_value = putn(input(value,f16.),'F',16,2);
when('N','D')     formatted_value = value;
otherwise;
end;
cards;
53.6 O
100 O
8 O
45238962 N
;;;;
run;
proc print;
run;``````

Super Contributor
Posts: 398

## Re: How to format variable in certain form?

Also, the result I run is still in Char., not Num.

Super User
Posts: 13,498

## Re: How to format variable in certain form?

A single column in a SAS dataset may only have one SAS display format applied.

If you are going to print these values it may be that you can use a custom format for ranges of values that will make you happy once the values are converted to numeric.

Perhaps something like:

proc format ;

value MixedDecimals

0 - 1000 = [F8.2]

1000 < - high = [best12.];

run;

/* the range I picked is pretty arbitrary as I do not know what your rule for range of value might be to force the decimal appearance

Note: with this format if your large values have decimals they will be displayed. If that is not wanted then replace Best12. with F12.0*/

Then print or other display using MixedDecimals format.

Proc print data=want;

var NumericValue;

format NumericValue MixedDecimals.;

run;

🔒 This topic is solved and locked.