Question:
You are given a text file called "stockprices.txt" containing information on the purchase and sale of stocks. The data layout is as follows:
A list of the data file is :
IBM 5/21/2006 $80.0 10007/20/2006 $88.5
CSCO04/05/2005 $17.5 20009/21/2005 $23.6
MOT 03/01/2004 $14.7 50010/10/2006 $19.9
XMSR04/15/2006 $28.4 20004/15/2007 $12.7
BBY 02/15/2005 $45.2 10009/09/2006 $56.8
Create a SAS data set (call it Stocks) by reading the data from this file. Use formatted input.
compute several new variable as follows:
Variable Description Computation
TotalPur Total purchase price Number times PurPrice
TotalSell Total selling price Number times SellPrice
Profit Profit TotalSell minus TotalPur
Print out the contents of this data set using PROC PRINT .
The following is my SAS code :
data Stocks;
infile "C:\Users\liaodong\Documents\chapter 3\stockprices.txt";
input
@1 Stock $4.
@5 PurDate mmddyy10.
@15 PurPrice dollar6.
@21 Number 4.
@25 SellDate mmddyy10.
@35 SellPrice dollar6.
;
TotalPur = Number*PurPrice;
TotalSell = Number*SellPrice;
Profit = TotalSell-TotalPur;
format PurPrice SellPrice TotalPur TotalSell Profit dollar10.
PurDate SellDate mmddyy10.;
run;
title "3.10 question";
proc print data = Stocks;
run;
The following is the log:
1068 data Stocks;
1069 infile "C:\Users\liaodong\Documents\chapter 3\stockprices.txt";
1070 input
1071 @1 Stock $4.
1072 @5 PurDate mmddyy10.
1073 @15 PurPrice dollar6.
1074 @21 Number 4.
1075 @25 SellDate mmddyy10.
1076 @35 SellPrice dollar6.
NOTE: The quoted string currently being processed has become more than 262 characters long. You
might have unbalanced quotation marks.
1077 ;
1078 TotalPur = Number*PurPrice;
1079 TotalSell = Number*SellPrice;
1080 Profit = TotalSell-TotalPur;
1069 infile "C:\Users\liaodong\Documents\chapter 3\stockprices.txt";
--
49
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS
release. Inserting white space between a quoted string and the succeeding identifier
is recommended.
1081 format PurPrice SellPrice TotalPur TotalSell Profit dollar10.
1082 PurDate SellDate mmddyy10.;
1083 run;
1084
1085 title "3.10 question";
1086 proc print data = Stocks;
1087 run;
data Stocks;
infile "C:\Users\liaodong\Documents\chapter 3\stockprices.txt";
input
@1 Stock $4.
@5 PurDate mmddyy10.
@15 PurPrice dollar6.
@21 Number 4.
@25 SellDate mmddyy10.
@35 SellPrice dollar6.
;
TotalPur = Number*PurPrice;
TotalSell = Number*SellPrice;
Profit = TotalSell-TotalPur;
format PurPrice SellPrice TotalPur TotalSell Profit dollar10.
PurDate SellDate mmddyy10.;
run;
title "3.10 question";
proc print data = Stocks;
run;
data Stocks;
infile "C:\Users\liaodong\Documents\My SAS Files\9.4\chapter 3\stockprices.txt";
input
@1 Stock $4.
@5 PurDate mmddyy10.
@15 PurPrice dollar6.
@21 Number 4.
@25 SellDate mmddyy10.
@35 SellPrice dollar6.
;
TotalPur = Number*PurPrice;
TotalSell = Number*SellPrice;
Profit = TotalSell-TotalPur;
format PurPrice SellPrice TotalPur TotalSell Profit dollar10.
PurDate SellDate mmddyy10.;
run;
title "3.10 question";
proc print data = Stocks;
run;
Do not (as in NOT) post data in pictures. Copy/paste your text data into a code box.
We cannot test your code against a picture.
And do not double-post. It does no good, and somebody needs to merge everything back into one thread.
Posting just SAS code only allows us to check the syntax and there is nothing significantly wrong. Please post your full SAS log including source statements, notes and errors as this should offer more clues as to what is happening.
"Does not work" on its own is a completely useless message. Please describe in detail what is happening; post the log from your step(s), and post an example of your input file.
data Stocks;
infile "**your path**\stockprices.txt";
input
@1 Stock: $4.
@5 PurDate: mmddyy10.
@15 PurPrice: dollar6.
@21 Number 4.
@25 SellDate: mmddyy10.
@35 SellPrice: dollar6.
;
TotalPur = Number*PurPrice;
TotalSell = Number*SellPrice;
Profit = TotalSell-TotalPur;
format PurPrice SellPrice TotalPur TotalSell Profit dollar10.
PurDate SellDate mmddyy10.;
run;
title "3.10 question";
proc print data = Stocks;
run;
Try to add colon after variables except number.
@tianerhu - Please focus on the first note of your log:
NOTE: The quoted string currently being processed has become more than 262 characters long. You might have unbalanced quotation marks.
You have an opening quote mark earlier in your program without a closing quote mark that is turning all of your code into one long character string. That's why it is not running as you expect. Fix that problem and your program will run a lot better.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.