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

I am trying to print the following data using formatted input.  I am unable to print the purchase price and selling price. Where am i going wrong? I have attached a screenshot of the data and also my output.

data Cache;
infile "/folders/myfolders/71442_example/stockprices.txt" dsd;
input 
@1 Stock_symbol $4.
@5 Purchase_date mmddyy10.
@15 Purchase_price 6.
@21 Number_of_shares 4.
@25 selling_date mmddyy10.
@35 selling_price 6.
;
format Purchase_date date9.;
format selling_date date9.;
format Purchase_price dollar11.2;
format selling_price dollar11.2;
run;

proc print data=Cache;
run;

 

D8.pngD9.png

1 ACCEPTED SOLUTION

Accepted Solutions
koyelghosh
Lapis Lazuli | Level 10

You can try this code.

 

data Cache;
*Change the infile path below
	infile "~/stockprices.txt" dsd pad;
	input 
@1 Stock_symbol $4.
@5 Purchase_date mmddyy10.
@15 Purchase_price dollar6.2
@21 Number_of_shares 4.
@25 selling_date mmddyy10.
@35 selling_price dollar6.2;
	format Purchase_date date9.;
	format selling_date date9.;
	format Purchase_price dollar6.2;
	format selling_price dollar6.2;
run;

proc print data=Cache;
run;

One of the problems was you wanted the output format of Purchase_price and selling_price to be 11.2 initially but the data width is only 6 characters (including dollar symbol) .. so we have to adjust.. Let me know if it works. 

 

Thanks

View solution in original post

12 REPLIES 12
Astounding
PROC Star

Parts of your INPUT statement are wrong:

 


@15 Purchase_price 6.

@35 selling_price 6.

Since the raw data contains dollar signs, you must tell SAS about it so it knows to ignore them:

 


@15 Purchase_price dollar6.


@35 selling_price dollar6.

Otherwise, the dollar signs will be invalid characters for a numeric variable.  The log probably displayed a message about invalid data.

koyelghosh
Lapis Lazuli | Level 10

@Astounding is right but looking at the Purchase Price and selling price, I think you want the width to be 11 and you want 2 decimal places for the cents as well (after dollar). So you should instead use the below:

 

@Astounding is right but looking at the Purchase Price and selling price, I think you want the width to be 11 and you want 2 decimal places for the cents as well (after dollar). So you should instead use the below:

data Cache;
	infile "~/stockprices.txt" dsd;
	input 
@1 Stock_symbol $4.
@5 Purchase_date mmddyy10.
@15 Purchase_price dollar11.2
@21 Number_of_shares 4.
@25 selling_date mmddyy10.
@35 selling_price dollar11.2;
	format Purchase_date date9.;
	format selling_date date9.;
	format Purchase_price dollar11.2;
	format selling_price dollar11.2;
run;

proc print data=Cache;
run;

 

koyelghosh
Lapis Lazuli | Level 10
Please replace the infile destination to where you want it to be .. I have used home directory ("~/") but you should change according to your infile path. Sorry about that
cy_th
Fluorite | Level 6

Thanks for your reply. I ran the code and I got this output. Why is it going wrong?

D10.png

koyelghosh
Lapis Lazuli | Level 10

Will you please paste the log here?

cy_th
Fluorite | Level 6

D11.pngD12.png

koyelghosh
Lapis Lazuli | Level 10
in the infile statement (just after dsd) if you add the option "PAD" .. does it help?
koyelghosh
Lapis Lazuli | Level 10

You can try this code.

 

data Cache;
*Change the infile path below
	infile "~/stockprices.txt" dsd pad;
	input 
@1 Stock_symbol $4.
@5 Purchase_date mmddyy10.
@15 Purchase_price dollar6.2
@21 Number_of_shares 4.
@25 selling_date mmddyy10.
@35 selling_price dollar6.2;
	format Purchase_date date9.;
	format selling_date date9.;
	format Purchase_price dollar6.2;
	format selling_price dollar6.2;
run;

proc print data=Cache;
run;

One of the problems was you wanted the output format of Purchase_price and selling_price to be 11.2 initially but the data width is only 6 characters (including dollar symbol) .. so we have to adjust.. Let me know if it works. 

 

Thanks

cy_th
Fluorite | Level 6

Yes!! It's working now. Thanks 🙂

koyelghosh
Lapis Lazuli | Level 10
If you like, you can mark the last post as a solution. All the best
ballardw
Super User

Instead of going through the extra steps of creating pictures of code or log just copy from the editor or log, open a code box on the forum with the {I} or "running man" icon and paste the text.

 

That way 1) you save time 2) you don't have to deal with code / log results that exceed a window with multiple pictures, 2) we can copy and paste relevant bits and show the changes needed in the code.

 

I know that I am not likely to retype a bunch of code to show one minor syntax change.

Tom
Super User Tom
Super User

Do NOT include decimal value in an INFORMAT unless you know that your source text has purposely NOT included the decimal point.

If you do that and the values being read do NOT include a decimal point then the resulting value will be divided by the appropriate power of ten to insert the implied decimal point that your informat specified.

 

It is generally easier to just add the TRUNCOVER option to handle short lines of data instead of asking SAS to pad the input buffer with spaces.  TRUNCOVER will also prevent SAS from going to a new line to look for data if a field is left blank.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 12 replies
  • 1653 views
  • 2 likes
  • 5 in conversation