BookmarkSubscribeRSS Feed
ChaoticSweets
Obsidian | Level 7

Hello! It is me again. 
I have been wrecking my brain around this sample question, and I'm confused with how to code it. 

I'm pretty sure im SUPER wrong, yet the code gets so close but has missing values due to some variables being uninitalized. 

 

Sample question;

Read sashelp.shoes as input.
• Create a new SAS data set, work.shoerange.
• Create a new character variable SalesRange that will be used to categorize the observations into
three groups.
• Set the value of SalesRange to the following:
o Lower when Sales are less than $100,000.
o Middle when Sales are between $100,000 and $200,000, inclusively.
o Upper when Sales are above $200,000.

Luckily it is with sashelp.shoes so everyone can do it (assuming its in your library.)

I have done so many renditions of my code but this is the only thing I've got closest to it;

MY CODE:

data work.shoerange;
	set sashelp.shoes;
run;
(I understand the above is probably useless but I did it anyway.)
data work.shoerange;
	set sashelp.shoes;
	*length SalesRange Sales Lower Middle Upper 8.;
	format SalesRange Sales Lower Middle Upper dollar12.;
	if SalesRange=Sales<100000 then Salesrange;
	else if SalesRange=100000<Sales<200000 then Salesrange=Middle;
	else if SalesRange=Sales>200000 then Salesrange=Upper;
	
	run;

No warnings or errors in log. However, it does mention that Lower, Middle, and Upper are initalized;

LOG NOTES:

 NOTE: Variable Lower is uninitialized.
 NOTE: Variable Middle is uninitialized.
 NOTE: Variable Upper is uninitialized.
 NOTE: There were 395 observations read from the data set SASHELP.SHOES.
 NOTE: The data set WORK.SHOERANGE has 395 observations and 11 variables.

How can I create a *new column named SalesRange with 3 other variables(Middle, Upper, and Lower) in that column to determine the range of sales? I'm pretty frustrated as I've been on this for about 2 hours now. I've used parenthesis. I did previously have middle as Sales<100000 and Sales > 100000 to attempt to keep to inclusive between the numbers to see if i could get anything.

 

I've also tried making separate tables in general using work.lower etc withthe data step and having set being the data set work.shoerange from the previous data step. However when I did that, only 1 or 2 tables would be filled, not all 3. 
Thank you! I tried to include everything i've done to see if I got close at all.


13 REPLIES 13
PaigeMiller
Diamond | Level 26

Is this what you are trying to do?

 

proc format;
     value sls_rng low-<100000 = 'Lower' 
       100000-<200000='Middle'
       200000-high='Upper';
run;

data work.shoerange;
	set sashelp.shoes;
	format SalesRange sls_rng.;
run;

If that's it, great. If not, please SHOW US the desired output.

 

Also, please go back to your original post and modify the subject line to describe the problem. Its not helpful to say "stuck on a sample question" because this doesn't  describe the problem, and virtually every message could have a subject line of "Stuck on a problem" and this would not be good. Please help us out by doing this, we're trying to help you out, let's help each other out.

--
Paige Miller
ChaoticSweets
Obsidian | Level 7
Hello, thank you for the response and the corrections! I was trying to fill this out asap and have corrected the Subject line to be more fitting to what the issue is.

I have tried the attempted code, (I did edit sls_rng to be SalesRange however in mine) and log mentions that SalesRange has been output.

I don't use proc format or the value statement so I will have to look that up, but it isn't what i'm attempting to do.
Sales Range is a new column with 3 categorical values using Middle, Lower, and Upper for Sales. (I hope i explained that correctly.)
PaigeMiller
Diamond | Level 26

@ChaoticSweets wrote:

I don't use proc format or the value statement so I will have to look that up, but it isn't what i'm attempting to do.
Sales Range is a new column with 3 categorical values using Middle, Lower, and Upper for Sales. (I hope i explained that correctly.)

This is exactly what custom format created in PROC FORMAT does for you. If you still don't like the output, then (I already asked once) SHOW US what you want.

--
Paige Miller
ChaoticSweets
Obsidian | Level 7

So when I've run the code, the log says 

 

 NOTE: Format SALESRANGE is already on the library WORK.FORMATS.
 NOTE: Format SALESRANGE has been output.

 

Sales Range is not in output data at all, I'm confused. Sorry I'm a bit slow, but how can I Have SalesRange in the output data so I can see the middle, upper, and lower values/variables? Am I able to omit value so I can create Sales Range? 

And how do I show you what I want when all I can do is (barely) explain it? I thought i said this earlier, so i apologize. I'm just trying to understand how to code this correctly since it's not referrable to my notes and the internet isn't helping much either..thank you. I have a basic idea of what it is telling me to write for the code. I'm not sure what else to tell you.

PaigeMiller
Diamond | Level 26

Is this is what you want? (a minor correction to my earlier code)

 

proc format;
     value sls_rng low-<100000 = 'Lower' 
       100000-<200000='Middle'
       200000-high='Upper';
run;

data work.shoerange;
	set sashelp.shoes;
	salesrange=sales;
	format SalesRange sls_rng.;
run;

I'm not asking you to explain further, I am asking you to show us what you want. Surely you must have in your mind what this desired output must look like. You can show us what you want by typing in an example of two or three rows of the data set.

 

--
Paige Miller
ChaoticSweets
Obsidian | Level 7

Oh that's what you meant! I'm so sorry. but yes, the code you just put did EXACTLY what I was attempting to accompish.

 

My example would've basically be what your code does here;

 

Sales      SalesRange

$2,202      Lower

$28,515    Lower

$116,333  Middle

$286,497  Upper

 

thank you so much! Next time i'll do better haha. 

ChaoticSweets
Obsidian | Level 7

sorry to bother you for one more quick question, I am attempting to filter the rows for where it only equals "Lower" under sales range. 

I am unable to do so because it says sales range is not in the original dataset Sashelp.shoes.

 

How do I filter through work.shoerange without getting that error ? I have tried several where statements and when I try to convert the column to a character type column, the proc format statement almost becomes null because that whole column would go missing (because I am attempting to look through that column.)
I hope i made sense somehow. thank you again for your wisdom

Reeza
Super User
Make sure you're pointing the WHERE clause to the new data set or you need to use IF.
WHERE filters data before it's read it, so cannot apply to new variables, in that case use IF.
You can use WHERE as a data set option to filter the output data set.

data work.shoerange (where=(salesRange="Lower"));
set sashelp.shoes;
length salesRange $10.;

if Sales<100000 then SalesRange = "Lower";
else if 100000<Sales<200000 then Salesrange="Middle";
else if Sales>200000 then Salesrange="Upper";

run;
ChaoticSweets
Obsidian | Level 7

I completely forgot I can do that as well. thank you so much for the hint!! 

ballardw
Super User

First failure:

ONE requirement is

• Create a new character variable SalesRange that will be used to categorize the observations into
three groups.

 If the first use of the variable name SalesRange is in a length statement with a numeric length definition such as:

Length SalesRange 8;

then you have defined it as numeric.

Or with a format like

Format SalesRange dollar12.;

will create the variable as numeric.

 

Next problem. You compare the VALUE of SalesRange to the Value of "Sales<10000". There is no reason for such a comparison because your rules are to ASSIGN a value to SalesRange(not compare) based only on the value of Sales:

o Lower when Sales are less than $100,000.
o Middle when Sales are between $100,000 and $200,000, inclusively.
o Upper when Sales are above $200,000.

None of the comparisons above include a comparison to SalesRange, only to Sales.

 

The conditional assignment to variable using IF looks like:

If <some condition is true> then variable=<the value to assign>;

See if you can spot the difference in the above compared to your code.

Character values are in quotes: "Middle" for example. Without the quotes SAS will think random words are variables.

Character variable lengths are specified with a $: Length variablename $15; would set the length of the variable to accept 15 characters. NOT the values, only the name of the variable.

ChaoticSweets
Obsidian | Level 7

I forgot about that! thank you ballard for the explaination. I was trying to figure how to sort out the missing values in several different ways...even if it seemed wrong. So that's on me. I appreciate the clairification!

Reeza
Super User
data work.shoerange;
	set sashelp.shoes;
        length salesRange $10.;
if Sales<100000 then SalesRange = "Lower"; else if 100000<Sales<200000 then Salesrange="Middle"; else if Sales>200000 then Salesrange="Upper"; run;

 

You know there's a free course that teaches you all these basics via video training? 

ChaoticSweets
Obsidian | Level 7

If it's the training courses, then I've finished those courses provided. However my learning curve is weird and I always have a harder time understanding if I can't ask additional questions or do additional work, where I always get more confused. 

 

Of course I do my best to review those additional videos or redo certain practices to make sure I thoroughly understand after I feel content with whatever I'm doing prior. However right now, I'm just not having a great time since statistics is a major weak point of mine. so the repetitive questions will happen because I'm persistent.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 13 replies
  • 2137 views
  • 1 like
  • 4 in conversation