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

i'm confusing in this question , and how to subtract ?
please help me out.

 

Run a single SQL program to update the values of variables. Add 5 more with weight if Type is Hamburger and subtract 100 from Calories. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The code works for me. You didn't include enough of your SAS log for us to see what error you might have made.

data food;
  length type $20 weight calories 8;
  input type weight calories ;
cards;
Hamburger 20 500
Hamburger 24 600
Chips 5 1000
;

proc print data=food;
 title 'BEFORE';
run;

proc sql;
update FOOD
  set weight=weight+5
    , calories=calories-100
  where type='Hamburger'
;
quit;

proc print data=food;
 title 'AFTER';
run;
BEFORE  

Obs    type         weight    calories

 1     Hamburger      20         500
 2     Hamburger      24         600
 3     Chips           5        1000

AFTER

Obs    type         weight    calories

 1     Hamburger      25         400
 2     Hamburger      29         500
 3     Chips           5        1000

View solution in original post

7 REPLIES 7
mkeintz
PROC Star

Re: "subtract":

 

In your select expression, you need to reduce the value of the calories field by 100.   I.e., you are being asked not only to list a bunch of fields from a table, but also to introduce a mathematical expression that would change the value of calories.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Abhau
Calcite | Level 5
yes exactly , please help me out
Abhau
Calcite | Level 5
This is my assignment , and this the question which i have , data sets i've by the name of junk food in that 8 variables , only the thing is , i've to update weight variable,as well as i've to subtract 100 from calories , in type variable Hamburger. please give some solution . sir
Tom
Super User Tom
Super User

@Abhau wrote:

i'm confusing in this question , and how to subtract ?
please help me out.

 

Run a single SQL program to update the values of variables. Add 5 more with weight if Type is Hamburger and subtract 100 from Calories. 


What I normally do on a test where the question is written in a confusing way is make an assumption about what they mean and explain clearly what question I am actually going to answer and then answer the clarified question.

So in this case if the question means:

Given a dataset name FOOD with character variable TYPE and numeric variables WEIGHT and CALORIES write a program using the SQL UPDATE statement that will for observations that have TYPE equal to 'Hamburger' change the value of WEIGHT to have 5 more (assuming that units are the same) and also update the value of CALORIES to have 100 less calories (assuming that values are already in calories).

Then an answer might look like:

proc sql;
update FOOD
  set weight=weight+5
    , calories=calories-100
  where type='Hamburger'
;
quit;
Abhau
Calcite | Level 5

this is the error which i'm getting , 
you gave me the code i used

 


ERROR 73-322: Expecting an =.

ERROR 76-322: Syntax error, statement will be ignored.

64 , calories=calories-100
65 where type='Hamburger';
66 quit;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds

 

Tom
Super User Tom
Super User

The code works for me. You didn't include enough of your SAS log for us to see what error you might have made.

data food;
  length type $20 weight calories 8;
  input type weight calories ;
cards;
Hamburger 20 500
Hamburger 24 600
Chips 5 1000
;

proc print data=food;
 title 'BEFORE';
run;

proc sql;
update FOOD
  set weight=weight+5
    , calories=calories-100
  where type='Hamburger'
;
quit;

proc print data=food;
 title 'AFTER';
run;
BEFORE  

Obs    type         weight    calories

 1     Hamburger      20         500
 2     Hamburger      24         600
 3     Chips           5        1000

AFTER

Obs    type         weight    calories

 1     Hamburger      25         400
 2     Hamburger      29         500
 3     Chips           5        1000
Abhau
Calcite | Level 5
wow thank you

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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