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

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:

tianerhu_1-1608944635886.png

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
tianerhu
Pyrite | Level 9
Thank you for your help, SASKiwi .

I close the SAS software, and restart it , then my code runs well.
I don't know why , I did't fix my code. Just restart SAS.

View solution in original post

16 REPLIES 16
tianerhu
Pyrite | Level 9
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;
tianerhu
Pyrite | Level 9

tianerhu_0-1608939426925.pngtianerhu_1-1608939445871.png

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;
SASKiwi
PROC Star

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.

Kurt_Bremser
Super User

"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.

Derek_Hu
Calcite | Level 5

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
Pyrite | Level 9
Thank you , but I think that is not right.
SASKiwi
PROC Star

@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.

tianerhu
Pyrite | Level 9
In my code, just one place using quote mark, I have a closing quote mark.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 16 replies
  • 2032 views
  • 0 likes
  • 4 in conversation