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

I would like to convert file sizes from MB, KB to Bytes. This can of course also be done via a formula, only I have used the format sizekmg. . Now I get for certain cases that it does not work, like with size_kb_2. Does anyone know why this is not working properly?

 

SAS Code:

data test;
size_kb_1="32.04KB";
size_kb_2="159.61KB";

size_bytes_1 = input(size_kb_1,sizekmg.);
size_bytes_2= input(size_kb_2,sizekmg.);
run;

 

Output dataset test:

Stephanvd_0-1632488371929.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data test;
size_kb_1="32.04KB";
size_kb_2="159.61KB";

size_bytes_1 = input(size_kb_1,sizekmg12.2);
size_bytes_2= input(size_kb_2,sizekmg12.2);
run;

View solution in original post

4 REPLIES 4
Ksharp
Super User
data test;
size_kb_1="32.04KB";
size_kb_2="159.61KB";

size_bytes_1 = input(size_kb_1,sizekmg12.2);
size_bytes_2= input(size_kb_2,sizekmg12.2);
run;
Tom
Super User Tom
Super User

Note that including the number of decimal places in an INFORMAT is telling SAS to divide any strings without an explicit decimal point by that power of ten.   Unless you know that the strings have expressly removed the decimal point to save one byte do not include the number of decimal places in the INFORMAT.  Just specify the total width.  And since INPUT() does not care if the informat width is larger than the length of the string it is reading you can just use the maximum allowed width.

sizekmg35.

 

Kurt_Bremser
Super User

Like the SIZEKMG format, the informat has a default width of 6, and therefore misses part of the string when you omit to specify a width.

So this works:

data test;
size_kb_1="32.04KB";
size_kb_2="159.61KB";
size_bytes_1 = input(size_kb_1,sizekmg12.);
size_bytes_2= input(size_kb_2,sizekmg12.);
run;

Note: do not use a fractional part in the informat unless you absolutely need it (when there's no dot in the input string, but the last x digits have to be considered as decimal fractions). A string of 1234, read with 6.2, would result in a number of 12.34.

 

Ksharp
Super User
Ha. Follow the Kurt's comment .
For the safety ,using "sizekmg12." ,NOT "sizekmg12.2" .

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 650 views
  • 5 likes
  • 4 in conversation