BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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;

 

 

 

 

 

 

 

View solution in original post

12 REPLIES 12
ybz12003
Rhodochrosite | Level 12

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

data_null__
Jade | Level 19

I interpret your question as.

 

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;

Capture.PNG

ybz12003
Rhodochrosite | Level 12

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

data_null__
Jade | Level 19

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

ybz12003
Rhodochrosite | Level 12

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".

data_null__
Jade | Level 19

 

 

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;
ybz12003
Rhodochrosite | Level 12

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.

 

 

ybz12003
Rhodochrosite | Level 12

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

data_null__
Jade | Level 19

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;

 

 

 

 

 

 

 

ybz12003
Rhodochrosite | Level 12

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

ballardw
Super User

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;

 

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
  • 12 replies
  • 1441 views
  • 3 likes
  • 4 in conversation