SAS Programming

DATA Step, Macro, Functions and more
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:

 

undefined

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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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