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;
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
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.
@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;
Thanks for your reply. I ran the code and I got this output. Why is it going wrong?
Will you please paste the log here?
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
Yes!! It's working now. Thanks 🙂
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.
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.
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!
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.