Pages

Saturday, 19 September 2015

SAS Problems

In this blog post I will be presenting problems of  SAS from a textbook written by Ron Cody.
The flow will be as such

  • Problem statement
  • Data description
  • Code
  • Result
  • Learning - what I have learnt from this problem

I am working on a permanent dataset with library name A15033
The link is below
libname A15033 "/folders/myfolders";


To access data sets click here

PROBLEM 1 :


Starting with the Blood data set, create a new, temporary SAS data set containing all the variables in Blood plus a new variable called CholGroup. Define this new variable as 
CholGroup 
Chol  low             low -110
          medium     111 - 140 
          high           141 - high
Use a SELECT statement to do this.

DATA DESCRIPTION;

We have a dataset named blood.txt. It contains id, name, bloodgroup, agegroup, wbc,rbc,chol.


CODE
libname A15033 "/folders/myfolders";
data A15033.blood;
infile "/folders/myfolders/blood.txt";
input id $ gender $ bloodgroup $ agegroup $ wbc rbc Chol;
 select;
 when (missing(Chol)) CholGroup = ' ';
 when (Chol le 110) CholGroup = 'Low';
 when (Chol le 140) CholGroup = 'Medium';
 otherwise CholGroup = 'High';
end; run;

proc print data=A15033.blood (obs=10);
run;





RESULT
Click on the image to enlarge



LEARNING

In this program I learned about how to assign group names based on their characteristics and how to treat missing values. 






PROBLEM 2;

 Using the Bicycles data set, list all the observations for Road Bikes that cost more than $2,500 or Hybrids that cost more than $660. The variable Model contains the type of bike and UnitCost contains the cost


DATA DESCRIPTION

This program is executed using datalines, So the data will be  in the program.


CODE


data bicycles;
input @1 country $15.
      @16 model   $15.
      @31 purpose $11.
      @42 quantity $6.
      @48 price dollar11.;
   
   
datalines;
USA             Road Bike      Trek       5000  $2200
USA             Road Bike      Cannondale 2000  $2100
USA             Mountain Bike  Trek       6000  $1200
USA             Mountain Bike  Cannondale 4000  $2700
USA             Hybrid         Trek       4500  $650
France          Road Bike      Trek       3400  $2500
France          Road Bike      Cannondale 900   $3700
France          Mountain Bike  Trek       5600  $1300
France          Mountain Bike  Cannondale 800   $1899
France          Hybrid         Trek       1100  $540
United Kingdom  Road Bike      Trek       2444  $2100
United Kingdom  Road Bike      Cannondale 1200  $2123
United Kingdom  Hybrid         Trek       800   $490
United Kingdom  Hybrid         Cannondale 500   $880
United Kingdom  Mountain Bike  Trek       1211  $1121
Italy           Hybrid         Trek       700   $690
Italy           Road Bike      Trek       4500  $2890
Italy           Mountain Bike  Trek       3400  $1877
;
run;


title "Selected Observations from BICYCLES";
proc print data=bicycles noobs;  
where model eq "Road Bike" and price gt 2500 or        
model eq "Hybrid" and price gt 660;  
run;


RESULT

LEARNING

In this program I have learned how to subset require information from data using "where" option



PROBLEM 3;

 Run the program here to create a temporary SAS data set called Vitals:
   data vitals;    
   input ID    : #3.             Age                   Pulse                 SBP             DBP;    
   label SBP = "Systolic Blood Pressure"          
            DBP = "Diastolic Blood Pressure";  
datalines;  
001 23 68 120 80  
002 55 72 188 96  
003 78 82 200 100  
004 18 58 110 70  
005 43 52 120 82  
006 37 74 150 98  
007  . 82 140 100    ;
Using this data set, create a new data set (NewVitals) with the following new variables:
    For subjects less than 50 years of age:
        If Pulse is less than 70, set PulseGroup equal to Low;    
        otherwise, set PulseGroup equal to High.      
        If SBP is less than 130, set SBPGroup equal to Low;      
        otherwise, set SBPGroup equal to High.
    For subjects greater than or equal to 50 years of age:
       If Pulse is less than 74, set PulseGroup equal to Low;      
       otherwise, set PulseGroup equal to High.      
       If SBP is less than 140, set SBPGroup equal to Low;      
       otherwise, set SBPGroup equal to High.
 assume there are no missing values for Pulse or SBP.


DATA DESCRIPTION

The data is given in the problem

CODE


data vitals;  
input ID    : $3. Age Pulse SBP DBP;  
label SBP = "Systolic Blood Pressure"        
      DBP = "Diastolic Blood Pressure";
datalines;
001 23 68 120 80
002 55 72 188 96
003 78 82 200 100
004 18 58 110 70
005 43 52 120 82
006 37 74 150 98
007  . 82 140 100
;
run;

data newvitals;  
set vitals;  
if Age lt 50 and not missing(Age) then do;    
   if Pulse lt 70 then PulseGroup = 'Low ';    
   else PulseGroup = 'High';    
   if SBP lt 140 then SBPGroup = 'Low ';    
   else SBPGroup = 'High';  
end;  
else if Age ge 50 then do;    
   if Pulse lt 74 then PulseGroup = 'Low';    
   else PulseGroup = 'High';    
   if SBP lt 140 then SBPGroup = 'Low';    
   else SBPGroup = 'High';  
end;
run;

title "Listing of NEWVITALS";
proc print data=newvitals noobs;
run;
 

RESULT

LEARNING
I have learned how to use nested if else statements along with do statement





PROBLEM 4:

 Modify the program here so that each observation contains a subject number (Subj), starting with 1: 
   data test;       
   input Score1-Score3;       
   /* add your line(s) here */    
   datalines;    
   90 88 92    
   75 76 88    
   88 82 91    
   72 68 70
   ;
   run;   

DATA DESCRIPTION

The date is given is in the program itself


CODE


data test;
 input Score1-Score3;
 Subj + 1;
 datalines;
 90 88 92
 75 76 88
 88 82 91
 72 68 70
 ;
 run;

 title "Listing of TEST";
 proc print data=test noobs;
 run;



RESULT



LEARNING

I learnt how to sum and list observations



PROBLEM 5;

 You have the following seven values for temperatures for each day of the week, starting with  Monday: 70, 72, 74, 76, 77, 78, and 85. Create a temporary SAS data    set (Temperatures) with a  variable (Day) equal to Mon, Tue, Wed, Thu, Fri, Sat, and    Sun and a variable called Temp equal to  the listed temperature values. Use a DO    loop to create the Day variable.



DATA DESCRIPTION

The data is in the program itself


CODE
data temperatures;
do Day = 'Mon','Tues','Wed','Thu','Fri','Sat','Sun';
 input Temp @;
 output;
 end;
 datalines;
 70 72 74 76 77 78 85
 ;
run;

 title "Listing of TEMPERATURES";
 proc print data=temperatures noobs;
 run;


RESULT
LEARNING

I learnt how to assign days in chronological order using do loop



PROBLEM  6:
 You invest $1,000 a year at 4.25% interest, compounded quarterly. How many   years will it take to reach $30,000? Do not use compound interest formulas. Rather,   use “brute force” methods with DO WHILE or DO UNTIL statements to solve this  problem.

DATA DESCRIPTION

The data is in the problem statement

CODE


data money;  
do Year = 1 to 999 until (Amount ge 30000);    
  Amount + 1000;    
  do Quarter = 1 to 4;        
    Amount + Amount*(.0425/4);    
    output;    
  end;  
end;  
format Amount dollar10.;
run;



title "Listing of MONEY";
proc print data=money (obs=10);
run;


RESULT

To view as a image I have made observations to 10 but the result will be a bit longer than that
LEARNING
I have learned how to use DO WHILE and DO UNTIL statements to calculate compound interest



PROBLEM 7;

 You have several lines of data, consisting of a subject number and two dates (date of   birth and visit date). The subject starts in column 1 (and is 3 bytes long), the date of birth starts in column 4 and is in the form mm/dd/yyyy, and the visit date starts in  column 14 and is in the form nnmmmyyyy (see sample lines below). Read the  following lines of data to create a temporary SAS data set called Dates. Format both  dates using the DATE9. format. Include the subject’s age at the time of the visit in  this data set.
0011021195011Nov2006  
0020102195525May2005  
0031225200525Dec2006


DATA DESCRIPTION

The data is given in the program itself

CODE


data dates;  
input @1  Subj  $3.        
      @4  DOB   mmddyy10.        
      @14 Visit date9.;  
Age = yrdif(DOB,Visit,'Actual');  
format DOB Visit date9.;
datalines;
00110/21/195011Nov2006
00201/02/195525May2005
00312/25/200525Dec2006
;


title "Listing of DATES";
proc print data=dates noobs;

run;


RESULT


LEARNING
I learnt how to calculate the age difference between two dates







PROBLEM 8:
 Using the SAS data set Blood, create two temporary SAS data sets called Subset_A and Subset_B.  Include in both of these data sets a variable called Combined equal to .001 times WBC plus RBC.  Subset_A should consist of observations from Blood where Gender is equal to Female and  BloodType is equal to AB. Subset_B should consist of all observations from Blood where Gender is  equal to Female, BloodType is equal to AB, and Combined is greater than or equal to 14.



DATA DESCRIPTION
The data set blood contains variables such as name, id , bloodgroup, agegroup, wbc, rbc, chol etc.




CODE

data subset_a;  
set mozart.blood;  
where Gender eq 'Female' and bloodgroup='AB';  
Combined = .001*WBC + RBC; run;


title "Listing of SUBSET_A";
proc print data=subset_a noobs;
run;


data subset_b;  
set mozart.blood;  
Combined = .001*WBC + RBC;  
if Gender eq 'Female' and bloodgroup='AB' and Combined ge 14;
run;


title "Listing of SUBSET_B";
proc print data=subset_b noobs;

run;

 

RESULT






LEARNING

In this program I learnt how to subset from a data set using where statement



PROBLEM 9;


Using the SAS data set Health, compute the body mass index (BMI) defined as the weight in kilograms divided by the height (in meters) squared. Create four other variables based on BMI: 1) BMIRound is the BMI rounded to the nearest integer, 2) BMITenth is the BMI rounded to the nearest tenth, 3) BMIGroup is the BMI rounded to the nearest 5, and 4) BMITrunc is the BMI with a fractional amount truncated. Conversion factors you will need are: 1 Kg equals 2.2 Lbs and 1 inch = .0254 meters.


DATA DESCRIPTION

The health data set consists of id, date of birth ,weight, height


CODE


libname A15033 "/folders/myfolders";
data A15033.health;
infile "/folders/myfolders/health.txt";
input @1 id $3.
      @4 dob mmddyy10.
      weight 14-16 height 17-18;
BMI = (Weight/2.2) / (Height*.0254)**2;  
BMIRound = round(BMI);  
BMIRound_tenth = round(BMI,.1);  
BMIGroup = round(BMI,5);  
BMITrunc = int(BMI);    
run;

title "listing of health";
proc print;
format dob date9.;

run;



RESULT


LEARNING

In this program I learned how to round and truncate integers






PROBLEM 10;

List the first 10 observations in data set Blood. Include only the variables Subject, WBC (white blood cell), RBC (red blood cell), and Chol. Label the last three variables “White Blood Cells,” “Red Blood Cells,” and “Cholesterol,” respectively. Omit the Obs column, and place Subject in the first column. Be sure the column headings are the variable labels, not the variable names. 


DATA DESCRIPTION

The data set blood contains variables such as name, id , bloodgroup, agegroup, wbc, rbc, chol etc.

CODE



data labelling;
set mozart.blood;
run;

title "First 10 Observations in BLOOD";
proc print data=mozart.blood(obs=10);
var   id wbc rbc Chol;  
label wbc = 'White Blood Cells'        
      rbc = 'Red Blood Cells'        
      Chol = 'Cholesterol';

run;


RESULT


LEARNING

In this program I learned how to label variable and column names


SAS Problems by "Ron Cody - A Programmers Guide"

In this blog post I will be presenting problems of SAS from textbook written by Ron Cody.
The presentation flow will be as such

  • Problem statement
  • Data description
  • Code
  • Result
  • Learning - what I have learnt from this problem
The library I am

SAS Problems by "Ron Cody - A Programmers Guide

In this 

Wednesday, 26 August 2015

STATISTICAL ANOMALY

ANOMALY :

  Something that deviates from what is normal, standard or expected.

 A statistical anomaly is an occurrence when something fall out of a normal scope for one group but at the same time it is not a result of being that group. 
 In web analytics anomalies detection plays a crucial role for site owners. For example here is a picture of a page views chart, generated by t.onthe.io. At 15.15 something happened, so there were no page views on a site. It is an anomaly, so the developers should find the problem and fix it.
The best example of a statistical anomaly is a person who has survived after a flight accident. Assuming there is a plane that crashed somewhere in the jungle. According to number of sold tickets, there were 30 passengers aboard. None of the passengers survived. While investigating plane crash, it appeared that one of the passengers missed departure time and didn’t get that flight.

So, this person changed the normal range of passengers group but not in a result of being a member of that group.

In fact, this lucky person is an example of statistical anomaly. Sometimes statistical anomaly is compared to guardian angel as it saves peoples lives.

Tuesday, 18 August 2015

SELLING A PEN

In "The Wolf of Wall Street" movie Jordan Belfort ( Leonardo DiCaprio ) has visually explained 'how to sell a pen'. We all are impressed by it (right!).
How???
Sell the need first..... Then sell the pen.
What if the person doesn't have the need or he pretends that he doesn't have a need???
 In an interview if we come across the question  "Sell me this pen?"
this is one of the several methods we can move forward.


For easy flow of lines assume I am being interviewed.
.......................................................................................

Interviewer: Do me a favor, sell me this pen. (reaches across to hand me the pen)

Me: (I slowly roll the pen between my index and thumb fingers.) When was the last time you used a pen?

Interviewer: This morning.

Me: Do you remember what kind of pen that was?

Interviewer: No.

Me: Do you remember why you were using it to write?

Interviewer: Yes. Signing a few new customer contracts.

Me: Well I’d say that’s the best use for a pen (we have a subtle laugh).

Wouldn’t you say signing those new customer contracts is an important event for the business? (nods head) Then shouldn’t it be treated like one. What I mean by that is, here you are signing new customer contracts, an important and memorable event. All while using a very unmemorable pen.

We grew up, our entire lives, using cheap BIC pens because they get the job done for grocery lists and directions. But we never gave it much thought to learn what’s best for more important events.

This is the pen for more important events. This is the tool you use to get deals done. Think of it as a symbol for taking your company to the next level. Because when you begin using the right tool, you are in a more productive state of mind, and you begin to sign more new customer contracts.

Actually. You know what? Just this week I shipped ten new boxes of these pens to Elon Musk’s office.

Unfortunately, this is my last pen today (reach across to hand pen back to CEO). So, I suggest you get this one. Try it out. If you’re not happy with it, I will personally come back next week to pick it up. And it won’t cost you a dime.
What do you say?

Interviewer: Yes.

...............................................................................................................................

Assume that interviewer is not generous as above
How we proceed......

.................................................................................................................................


ME: I was just curious to know which pen do you use currently?

Interviewer: Why do you care?


ME: I work for a company that produces pens that are optimized for better handwriting and faster writing, if you are already not using our pen, I would like to show it to you.

Interviewer: I use "X brand", I am happy with it, I don't need your pen, thanks.


ME: Sure, X brand is good too, but do you know your handwriting could improve twice as much by simply switching to our pen, here is my pen, why don't you try writing something yourself, here is the paper, see the magic yourself.

Interviewer: No really, I don't need another pen.


ME: That's no problem, you don't need to buy, if you like it maybe you will tell your friends or gift some child you know who has bad handwriting, you know bad handwriting affects grades in schools, you would be doing someone a favor just by recommending

ME: Just a sentence, this is magic, just see the improvement in your now handwriting

Interviewer (writes): Ok, not bad, my writing does look cleaner


ME: Yes, and you know what, the more you write with this, the better your handwriting will be

Interviewer:Cool, let me take one of these, i can gift this to my nephew


ME: Great idea, since this is my first meeting with you, let me give you a discount of 10% if you buy for your friends and family too, if you buy 5 or more you get a straight 10% off, buy 10 and get 20% off, just this time.

Interviewer: Ah I don't know, ok i'll buy 5 buy give me 15% discount.


ME: Wish I could, that's beyond my authority, fine, if you can buy 7 you get 15%, please say yes and seal the deal ;-)

Interviewer: Haha, alright, deal.


ME: Ah by the way, this pen comes with special Ink too, if you buy one bottle...




If we can sell a pen; we can sell anything( I guess) :)






Tuesday, 11 August 2015

CORREALTION AND CAUSATION

Correlation can also be called association. When a correlation is found between two variables, it is normally presumed that one causes another. But it is not the case always. In statistics there are lot more variables that should taken into account.
For example we go with a study
   Children who eat breakfast have higher grades, so eating breakfast causes higher grades.
There may be a third factor causing both higher grades and eating breakfast such as more responsible and loving parents who help children with homework as well as make sure they eat breakfast.  Causation can only be inferred from a randomised experiment.
The below video gives a complete explanation of correlation and causation and other things that should taken into consideration before jumping into conclusions.

Monday, 10 August 2015

Osama Bin Laden

We all have played Cricket...... Who is the World's greatest batsman?? Sachin Tendulkar, Don Bradman... we can name a few right. We all have played Hide and Seek....... Who is the greatest Hide and Seek player the world has ever seen???? OSAMA BIN LADEN. Being born in one of the richest families in Saudi and having lot of political connections he completed major part of his higher education in U.S.Putting aside all the bad things he did to this World let us focus on his great capabilities. 
*  He worked for C.I.A in the early stage of his career, he understood how C.I.A operates, in fact how any intelligence agency operates ; so he has been evading the manhunt for decades.-"Analytical skills". 
*  He taught himself Nuclear science - one of the most difficult subjects on the Planet -        "Intelligence and Concentration".
* He is hiding in Afghanistan and his men working and dying for him all over the globe -"Leadership and Motivational skills". 
Had been if he used his skills other way around; the World would have been a better place to live.

P,S.- This is the topic given to me in extempore and above is the way I presented it.