BookmarkSubscribeRSS Feed
rkaur17
Calcite | Level 5

I tired correcting this code I was given. However, it is not working. How should I go about correcting it? 

INITIALLY GIVEN:

 

OPTIONS PS = 58 LS = 8 NODAT NONUMBER

DATA sales;

    INPUT weekof mmddyy8. Store $ Mon Tues Wed Thur Fri; AvgSales = (Mon + Tues +

Wed + Thurs + Fri)/4; length Group $6;

lable weekof = 'Date;

if AvgSales = '.' then Group = 'N/A'; else if AvgSales LE 605 then Group = 'Low';

else if 605 LT AvgSales LE 750 then Group = 'Average'; else if AvgSales GT 75O

then Group = 'High'; if Store = 110 then region = 'South'; else if Store = 111 then

region = 'South';

 

GOAL IS TO GET THE FOLLOWING RESULTS - attached 

 

 

else if Store = 112 then region = 'North';

else if Store = 113 then region = 'North';

else if Store = 114 then region = North;

drop Mon Tues Wed Thur Fri;

DATALINES

10/12/07 11O 412 532 641 701 802

10/12/07 111 478 567 699 789 821

10/12/07 112 399 501 650 712 812

10/12/07 113 421 532 698 756 872

10/12/07 114 401 510 612 721 899

17/12/07 110 710 725 789 721 799

17/12/07 111 689 701 729 703 721

17/12/07 112 899 812 802 738 712

17/12/07 113 700 712 748 765 801

17/12/07 114 699 799 899 608   

24/12/07 110 340 333 321 401 490

24/12/07 111 801 793 721 763 798

24/12/07 112 598 798 684 502 412

24/12/07 113 980 921 832 812 849

24/12/07 114 798 709 721 799 724

31/12/07 110 487 321 399 312 321

31/12/07 111 501 532 598 581 601

31/12/07 112 598 512 540 523 549

31/12/07 113 601 625 674 698 601

31/12/07 114 900 805 700 601 811

;

RUN; PROC PRINT data = sales / label; RUN;

 

HOW I HAVE CORRECTED IT SO - STILL NOT WORKING 

 

DATA sales;
input Date $ 1-8 Store 10-12 Mon 14-16 Tues 18-20 Wed 22-24 Thurs 26-28 Fri 30-32;
DATALINES;
10/12/07 110 412 532 641 701 802
10/12/07 111 478 567 699 789 821
10/12/07 112 399 501 650 712 812
10/12/07 113 421 532 698 756 872
10/12/07 114 401 510 612 721 899
17/12/07 110 710 725 789 721 799
17/12/07 111 689 701 729 703 721
17/12/07 112 899 812 802 738 712
17/12/07 113 700 712 748 765 801
17/12/07 114 699 799 899 608 .
24/12/07 110 340 333 321 401 490
24/12/07 111 801 793 721 763 798
24/12/07 112 598 798 684 502 412
24/12/07 113 980 921 832 812 849
24/12/07 114 798 709 721 799 724
31/12/07 110 487 321 399 312 321
31/12/07 111 501 532 598 581 601
31/12/07 112 598 512 540 523 549
31/12/07 113 601 625 674 698 601
31/12/07 114 900 805 700 601 811
;
AvgSales=(Mon + Tues + Wed + Thurs + Fri)/5; length Group $6; label week ='Date;
if (AvgSales = '.') then Group = N/A;
else if (AvgSales <= 605) then Group = Low;
else if (AvgSales 605 < sales <=750) then Group = Average;
else if (AvgSales sales >750) then Group = High;
if Store = 110 then region = South;
else if Store = 111 then region = South;
else if Store = 112 then region = North;
else if Store = 113 then region = North;
else if Store = 114 then region = North;
drop Mon Tues Wed Thur Fri;
OPTIONS PS = 58 LS = 8 NODAT NONUMBER
RUN;
PROC PRINT data=sales / label;
RUN;

2 REPLIES 2
Tom
Super User Tom
Super User

The SAS log should show you where your errors are.  Have you tried to correct those?  Remember to fix the first error.  Later errors might just be caused by the SAS compiler being confused because of the first error.

 

Is there some specific error in the log that you cannot figure out?

If so then copy of and paste the log for that step.  Make sure to use the Insert Code button on the editor so that you can paste the lines from the log without the forum treating them as paragraphs.

ballardw
Super User

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

 

BTW you have a couple of issues that are admittedly newbie issues. SAS, unlike many general programming languages, has FUNCTIONS for a number of statistics such as MEAN, STD, SUM.

If you  have missing values for any of the variables when you do A+B+C then the result will be missing. This is intentional documented behavior of the + operator. If you might have a missing value but want the sum of the non-missing values then use SUM(A,B,C).

So you may want one of either

AvgSales= sum(Mon , Tues , Wed , Thurs , Fri)/5;
if you want average per day sales
or perhaps

AvgSales= mean(Mon, Tues, Wed, Thurs, Fri); 
if you want average sales per day for the days with sales.

The missing value is a numeric value for missing when using the period or dot. You should not enclose it quotes.

if (AvgSales = '.') then Group = N/A;

Should be throwing LOTS of errors because you probably do not have a variable named N/A and likely are intending to create a text variable named group with the value of "N/A" Which would be done with

if (AvgSales = .) then Group = 'N/A';

Similarly ALL the values of Group need to be inside quotes. You also will have an issue I the Group variable not having a defined length. So the first use in the code of Group = 'N/A' would set the length to 3 characters and the remaining values would be truncated.

You need to specify a length for character variables before use in many cases to prevent this using the LENGTH statement before the variable is used in the code.

 

Length Group $ 7; creates a variable of 7 characters.

Similar with your Region variable. Those should be character values in quotes instead of just South or North.

 

There is another comparison operator called IN that allows you to compare multiple values to simplify some of the code:

Instead of

if Store = 110 then region = 'South';
else if Store = 111 then region = 'South;'
else if Store = 112 then region = 'North';
else if Store = 113 then region = 'North';
else if Store = 114 then region = 'North';

You can use

if Store in (110 111) then region='South';
else if Store in (112 113 1140 then region = 'North';

 

A slightly more advanced approach is to create a custom format to assign text based on a single variable value.

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!

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
  • 2 replies
  • 650 views
  • 1 like
  • 3 in conversation