*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

***   c06s1d1.sas   ***;

dm "output;clear;log;clear";

 

Libname Course2 "C:\_SasCourse2006\Course2\SAS_Data"

 

data work.agents2;

   set Course2.agents;

   length Country $ 20 TrueLocation $ 40;

   Country=scan(CityCountry,2,',');

   if Country='USA'

      then TrueLocation = scan(CityCountry,1,',')

            !! ', ' !! State;

   else /* not USA */

      TrueLocation = CityCountry;

run;

 

proc print data=work.agents2 noobs;

   var CityCountry TrueLocation State;

   title 'Current Output from Program';

run;

 

/* Insert a PUT statement to see if the code is executing */

dm "output;clear;log;clear";

data work.agents2;

   set Course2.agents;

   length Country $ 20 TrueLocation $ 40;

   Country=scan(CityCountry,2,',');

   if Country='USA' then do;

      TrueLocation = scan(CityCountry,1,',')

          !! ', ' !! State;

      put 'Country is USA';

   end;

   else /* not USA */

      TrueLocation = CityCountry;

run;

 

/* Check the value for Country just before the IF-THEN statement */

dm "output;clear;log;clear";

data work.agents2;

   set Course2.agents;

   length Country $ 20 TrueLocation $ 40;

   Country=scan(CityCountry,2,',');

   put Country=;

   if Country='USA' then do;

      TrueLocation = scan(CityCountry,1,',')

         !! ', ' !! State;

      put 'Country is USA';

   end;

   else /* not USA */

      TrueLocation = CityCountry;

run;

 

/* Use $quote. format to check for leading blanks */

dm "output;clear;log;clear";

data work.agents2;

   set Course2.agents;

   length Country $ 20 TrueLocation $ 40;

   Country=scan(CityCountry,2,',');

   put Country $quote20.;

   if Country='USA' then do;

      TrueLocation = scan(CityCountry,1,',')

         !! ', ' !! State;

      put 'Country is USA';

   end;

   else /* not USA */

      TrueLocation = CityCountry;

run;

 

/* Use LEFT function to remove leading blanks and check code again */

dm "output;clear;log;clear";

data work.agents2;

   set Course2.agents;

   length Country $ 20 TrueLocation $ 40;

   Country=left(scan(CityCountry,2,','));

   put Country $quote20.;

   if Country='USA' then do; /*Note space before USA*/

         TrueLocation = scan(CityCountry,1,',')

            !! ', ' !! State;

      put 'Country is USA';

   end;

   else /* not USA */

      TrueLocation = CityCountry;

run;

 

proc print data=work.agents2 noobs;

   var TrueLocation CityCountry State;

   title 'Output with Leading Spaces Removed';

run;

 

/* Delete PUT statements */

dm "output;clear;log;clear";

data work.agents2 (drop=country);

   set Course2.agents;

   length Country $ 20 TrueLocation $ 40;

   Country=left(scan(CityCountry,2,','));

   if Country='USA' then /*Note the space before USA*/

      TrueLocation = scan(CityCountry,1,',')

         !! ', ' !! State;

   else /* not USA */

      TrueLocation = CityCountry;

run;

 

proc print data=work.agents2 noobs;

   var TrueLocation CityCountry State;

   title 'Corrected Output';

run;

 

 

 

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

***   c06s2d1.sas   ***;

dm "output;clear;log;clear";

 

Libname Course2 "C:\_SasCourse2006\Course2\SAS_Data"

 

data work.agents2;

   set Course2.agents;

   length Country $ 20 TrueLocation $ 40;

   Country=scan(CityCountry,2,',');

   if Country='USA'

      then TrueLocation = scan(CityCountry,1,',')

            !! ', ' !! State;

   else /* not USA */

      TrueLocation = CityCountry;

run;

 

proc print data=work.agents2 noobs;

   var TrueLocation CityCountry State;

   title 'Locations of Ticket Agents';

run;

 

/* Use DEBUG option to see if conditional logic is executing and

   the value of country before IF-THEN statement */

dm "output;clear;log;clear";

data work.agents2/debug;

   set Course2.agents;

   length Country $ 20 TrueLocation $ 40;

   Country=scan(CityCountry,2,',');

   if Country='USA'

      then TrueLocation = scan(CityCountry,1,',')

            !! ', ' !! State;

   else /* not USA */

      TrueLocation = CityCountry;

run;

 

 


/* Remove the DEBUG option, use the LEFT function to remove

   leading spaces and drop CityCountry and Country */

dm "output;clear;log;clear";

data work.agents2(drop=Country);

    set Course2.agents;

    length Country $ 20 TrueLocation $ 40;

    Country=left(scan(CityCountry,2,','));

    if Country='USA'

       then TrueLocation = scan(CityCountry,1,',')

            !! ', ' !! State;

    else /* not USA */

       TrueLocation = CityCountry;

run;

 

proc print data=work.agents2 noobs;

   var TrueLocation CityCountry State;

   title 'Locations of Ticket Agents';

run;

 

 

 

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

***   c07s1d1.sas   ***;

dm "output;clear;log;clear";

 

Libname Course2 "C:\_SasCourse2006\Course2\SAS_Data"

 

data invest;

   do Year=2001 to 2003;

      Capital+5000;

      Capital+(Capital*.075);

      output;

   end;

run;

 

proc print data=invest noobs;

   title1 'Results of investing 5000 dollars for';

   title2 'three years at 7.5 percent interest.';

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

***   c07s1d2.sas   ***;

data forecast;

   set Course2.growth(rename=(NumEmps=NewTotal));

   do Year=1 to 3;

      NewTotal=NewTotal*(1+Increase);

      output;

   end;

run;

 

proc print data=forecast noobs;

   title1 'Example of a DO loop';

   title2 'to eliminate the redundant code';

run;

 

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

***   c07s1d3.sas   ***;

data invest(drop=Quarter Year);

   set Course2.Banks;

   Capital=0;

   do Year=1 to 5;

      Capital+5000;

      do Quarter=1 to 4;

         Capital+(Capital*(Rate/4));

      end;

   end;

run;

 

proc print data=invest noobs;

   title1 'Results of investing 5000 dollars for';

   title2 'five years in three different banks that compound quarterly.';

run;

 

 

 

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

***   c07s2d1.sas   ***;

dm "output;clear;log;clear";

 

Libname Course2 "C:\_SasCourse2006\Course2\SAS_Data"

 

data charity(drop=Qtr);

   set Course2.donate;

   array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4;

   do Qtr=1 to 4;

      Contrib{Qtr}=Contrib{Qtr}*1.25;

   end;

run;

 

proc print data=charity noobs;

   title 'Contributions after 25 percent supplement';

run;

 

 

 


*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

***   c07s3d1.sas   ***;

dm "output;clear;log;clear";

 

Libname Course2 "C:\_SasCourse2006\Course2\SAS_Data"

 

data percent(drop=Qtr);

   set Course2.donate;

   Total=sum(of Qtr1-Qtr4);

   array Contrib{4} Qtr1-Qtr4;

   array Percent{4};

   do Qtr=1 to 4;

      Percent{Qtr}=Contrib{Qtr}/Total;

   end;

run;

 

proc print data=percent noobs;

   title1 "Percentage that each quarter's contribution";

   title2 'represents of the total annual contribution';

   var Id Percent1-Percent4;

   format Percent1-Percent4 percent6.;

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

***   c07s3d2.sas   ***;

data change(drop=i);

   set Course2.donate;

   array Contrib{4} Qtr1-Qtr4;

   array Diff{3};

   do i=1 to 3;

      Diff{i}=Contrib{i+1}-Contrib{i};

   end;

run;

 

proc print data=change noobs;

   title1 "Change in each quarter's contribution";

   var ID Diff1-Diff3;

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

***   c07s3d3.sas   ***;

data compare(drop=Qtr);

   set Course2.donate;

   array Contrib{4} Qtr1-Qtr4;

   array Diff{4};

   array Goal{4} _temporary_ (10,15,5,10);

   do Qtr=1 to 4;

      Diff{Qtr}=Contrib{Qtr}-Goal{Qtr};

   end;

run;

 

proc print data=compare noobs;

   title1 'Comparison of actual employee contributions';

   title2 'versus expected contributions';

   var Id Diff1 Diff2 Diff3 Diff4;

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

***   c07s3d4.sas   ***;

data rotate(drop=Qtr1-Qtr4);

   set Course2.Donate;

   array Contrib{4} Qtr1-Qtr4;

   do Qtr=1 to 4;

      Amount=Contrib{Qtr};

      output;

   end;

run;

 

proc print data=rotate noobs;

   title 'Data set rotation using SAS array processing';

run;

 

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

***   c08s1d1.sas   ***;

dm "output;clear;log;clear";

 

Libname Course2 "C:\_SasCourse2006\Course2\SAS_Data"

 

data newtrans

     noactiv (drop=Trans Amnt)

     noacct  (drop=Branch);

   merge Course2.transact(in=InTrans)

         Course2.branches(in=InBanks);

   by ActNum;

   if intrans and inbanks then output NewTrans;

   else if inbanks and not intrans then output noactiv;

   else if intrans and not inbanks then output noacct;

run;

 

proc print data=newtrans noobs;

   title 'Account Transactions for Current Week';

run;

 

proc print data=noactiv noobs;

   title 'No Activity on These Accounts';

run;

 

proc print data=noAcct noobs;

   title 'Bad Account Number';

run;

 

*---+----1----+----2----+----3----+----4----+----5----+----6----+---;

***   c08s2d1.sas   ***;

dm "output;clear;log;clear";

 

Libname Course2 "C:\_SasCourse2006\Course2\SAS_Data"

 

proc sql;

   title 'Join -- No WHERE clause';

   select *

      from Course2.transact, Course2.branches

      ;

 

title 'Inner Join';

   select Transact.ActNum, Trans, Amnt, Branch

      from Course2.transact, Course2.branches

      where Transact.ActNum=Branches.ActNum

      ;

 

title 'Inner Join with a WHERE Using an Alias';

   select Trans.ActNum, Trans, Amnt, Branch

      from Course2.transact as Trans, Course2.branches as Banks

      where Trans.ActNum=Banks.ActNum

      ;

quit;