DATA Step, Macro, Functions and more

In SAS IF is No then process

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

In SAS IF is No then process

I am trying to use two variable : internet = Yes or No , Number of days internet used=number value. If yes then calculate activation charges 10$ and each day internet charges $5. I am not able to get results 

 

I am using code :

 

data format;
If Internet = 'No' then Do;
if Internet= 'Yes' then (InternetDays*4.95)+9.95;
input IRGA =;
run;

proc print data=format;
run;


Accepted Solutions
Solution
‎10-10-2016 01:21 PM
Super User
Super User
Posts: 7,392

Re: In SAS IF is No then process

Just to add to @Reeza's excellent advice, whilst learning Base SAS, you can also learn various functions which will improve programming efficiency, one of which is the ifn/ifc function, for example:

data want; 
  set have;
  internet_cost=ifn(upcase(internet)="YES",(internetdays*4.95)+9.95,0);
  format internet_cost dollar12.2;
run;

Also, some other tips - calling a dataset format is not a good idea unless it is a formats dataset.  Not forgetting consistent casing, indentation, finishing code blocks (you didn't use and end for the do in your first example for instance).  

View solution in original post


All Replies
Super User
Posts: 17,737

Re: In SAS IF is No then process

1. You need. SET statement so you have an input dataset to process. 

2. Input is used to read files. That's not applicable in this code. Not sure what you're trying to do with that line. 

3. You're doing a calculation but not saving it to a variable. 

 

All your mistakes are basic SAS programming skills. SAS offers the first programming course for free via an ecourse. I highly recommend you complete the course. There's also an abundance of videos on basic task. 

 

http://support.sas.com/training/tutorial/

 

Data Format2; *USE A NEW DATASET NAME TO NOT OVERWRITE PREV DATA SET. THIS HELPS AVOID ERRORS;

SET FORMAT; * ASSIGN INPUT DATA SET;

if Internet= 'Yes' then INTERNET_COST = (InternetDays*4.95)+9.95; * STORE RESULTS IN VARIABLE;

run;

*PRINT RESULTS TO WINDOW FOR DISPLAY;
proc print data=format2;
run;
Contributor
Posts: 34

Re: In SAS IF is No then process

dear reeza,

 

I want to use both internet = No and Internet=Yes and if No then consider internet_cost=0 and if yes then consider

if Internet= 'Yes' then INTERNET_COST = (InternetDays*4.95)+9.95;

 but if internet =NO then Internet_cost=0 .

 

so the code would be below , plz advise , and also how to add $ sign in the column result?

 

Data Format2; 
SET FORMAT; 
if Internet=No then Internet_Cost = 0 if Internet= 'Yes' then INTERNET_COST = (InternetDays*4.95)+9.95; * STORE RESULTS IN VARIABLE; run; *PRINT RESULTS TO WINDOW FOR DISPLAY; proc print data=format2; run;

 

Super User
Posts: 17,737

Re: In SAS IF is No then process

Make sure the No is in quotation marks since it's a string. 

 

To add $ sign format the variable  using dollar12.2 

Contributor
Posts: 34

Re: In SAS IF is No then process

Dear Reeze,

if Internet= 'Yes' then INTERNET_COST = (InternetDays*4.95)+9.95; THIS IS COMING EMPTY COLUMN. there are days in InternetDays but it is not * with 4.95 and adding to 9.95 .

Secondly where i should use dollar12.2 , like where i am using Internet_cost dollar12.2 in if statement or i have use it separately for SAS to read .

Super User
Posts: 17,737

Re: In SAS IF is No then process

Post an example of your first case. Most likely it's not Yes, since it has to be an exact match. yes, YES are not the same, string comparisons are case sensitive. If it's something like this use the UPCASE function to convert everything to all caps. 

 

If upcase(internet) = 'YES' ....

 

For the dollar sign apply a format, same as you did fo date variables to control the display. 

 

Format internet_cost dollar12.2;

 

As I mentioned these are base concepts. Please consider taking that course. 

 

 

Solution
‎10-10-2016 01:21 PM
Super User
Super User
Posts: 7,392

Re: In SAS IF is No then process

Just to add to @Reeza's excellent advice, whilst learning Base SAS, you can also learn various functions which will improve programming efficiency, one of which is the ifn/ifc function, for example:

data want; 
  set have;
  internet_cost=ifn(upcase(internet)="YES",(internetdays*4.95)+9.95,0);
  format internet_cost dollar12.2;
run;

Also, some other tips - calling a dataset format is not a good idea unless it is a formats dataset.  Not forgetting consistent casing, indentation, finishing code blocks (you didn't use and end for the do in your first example for instance).  

Contributor
Posts: 34

Re: In SAS IF is No then process

Thankyou very much. Your example really helped my code . Its long code and i was sharing a small part of it , so was not able to get right answers . But your example helped me to get results error free.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 422 views
  • 0 likes
  • 3 in conversation