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;
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).
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;
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;
Make sure the No is in quotation marks since it's a string.
To add $ sign format the variable using dollar12.2
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.
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).
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!
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.