DATA Step, Macro, Functions and more

Percents and Dollar values in datalines

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 124
Accepted Solution

Percents and Dollar values in datalines

I'm trying to manually generate data with the datalines statement. I can't seem to figure out how to properly get correct values for percents and dollar values. This is what I have right now. Have tried different formats but can't get it to work.

 

data test;
  infile datalines;
  format Current_Cost dollar3.2 Target percent8.2;
  input Current_Cost dollar3.2 Target percent8.2;
  datalines;
$40.00 76.41%
$30.00 76.21%
$20.00 50.36%
$10.00 20.22%
$23.00 24.91%
$45.00 86.12%
;;
run;

I want them to display exactly as they are in the datalines. But right now it is coming out as this:

 

Capture.PNG


Accepted Solutions
Solution
‎04-27-2017 02:40 PM
Respected Advisor
Posts: 2,658

Re: Percents and Dollar values in datalines

Posted in reply to JediApprentice

First, get rid of the unneeded characters in your inputs, so that the inputs are entirely numeric (and a decimal point).

 

Then, you don't need the informats in the INPUT statement.

 

If you want the dollars to come out as $40.00 then you want format DOLLAR5.2 (or bigger, such as DOLLAR10.2, if you will have many more digits to the LEFT of the decimal point.)

 

I leave it up to you to play with the format for percent until you get it to work the way you want it.

--
Paige Miller

View solution in original post


All Replies
Solution
‎04-27-2017 02:40 PM
Respected Advisor
Posts: 2,658

Re: Percents and Dollar values in datalines

Posted in reply to JediApprentice

First, get rid of the unneeded characters in your inputs, so that the inputs are entirely numeric (and a decimal point).

 

Then, you don't need the informats in the INPUT statement.

 

If you want the dollars to come out as $40.00 then you want format DOLLAR5.2 (or bigger, such as DOLLAR10.2, if you will have many more digits to the LEFT of the decimal point.)

 

I leave it up to you to play with the format for percent until you get it to work the way you want it.

--
Paige Miller
Super User
Posts: 13,066

Re: Percents and Dollar values in datalines

[ Edited ]
Posted in reply to JediApprentice

For your exampe:

data test;
  infile datalines truncover;
  format Current_Cost dollar6.2 Target percent8.2;
  input Current_Cost dollar6.2 Target percent8.2 ;
  datalines;
$40.00 76.41%
$30.00 76.21%
$20.00 50.36%
$10.00 20.22%
$23.00 24.91%
$45.00 86.12%
;;
run;

 

Problems: when you say 3.2 you mean read a value with a length of 3 characters and 2 of them are decimals. So a Dollar3.2 format has lots of problems as the $ takes one position, the . takes a second and that doesn't leave much. You need to count every likely displayed character in the w part of a format that has the w.d option to set the W part. Your informat of percent failed because you requested 8 characters but the value only had 6. So the line of data ran out. The TRUNCOVER option says to read what is present and truncate the data.

Frequent Contributor
Posts: 124

Re: Percents and Dollar values in datalines

Okay this is the final solution - I changed the values of the percents. Thank you both for your help.

 

data test;
  infile datalines;
  format Current_Cost dollar6.2 Target percent8.2;
  input Current_Cost Target;
  datalines;
40 .7641
30 .7621
20 .5036
10 .2022
23 .2491
45 .8612
;;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 249 views
  • 1 like
  • 3 in conversation