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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

7 REPLIES 7
Reeza
Super User

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;
azee007
Calcite | Level 5

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;

 

Reeza
Super User

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

 

To add $ sign format the variable  using dollar12.2 

azee007
Calcite | Level 5
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 .

Reeza
Super User

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. 

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

azee007
Calcite | Level 5
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1321 views
  • 0 likes
  • 3 in conversation