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

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

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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
ballardw
Super User

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.

JediApprentice
Pyrite | Level 9

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;

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
  • 3 replies
  • 2183 views
  • 1 like
  • 3 in conversation