Monday, May 7, 2018

R: Scope of variables within user defined functions



R: Scope of variables within user defined functions


Source: https://stackoverflow.com/questions/10904124/global-and-local-variables-in-r

Variables declared inside a function are local to that function. For instance:

foo <- function() {
   bar <- 1
}
foo()
bar

gives the following error: Error: object 'bar' not found.


If you want to make bar a global variable, you should do:

foo <- function() {
    bar <<- 1
}
foo()
bar

In this case bar is accessible from outside the function.



However, unlike C, C++ or many other languages, brackets do not determine the scope of variables. For instance, in the following code snippet:

if (x > 10) {
    y <- 0
}
else {
    y <- 1
}

y remains accessible after the if-else statement.



PowerShell: Enable execution of PS scripts of local machine




PowerShell: Enable execution of PS scripts of local machine


Open command window with Run As Administrator

Type powershell and press Enter.

Get-ExecutionPolicy -List
Set-ExecutionPolicy Bypass
Get-ExecutionPolicy -List  (you will see LocalMachine is now Bypass)

exit


(There is a scope parameter to specify other than LocalMachine.)

R: ts function gotchas...




R: ts function gotchas...



> # It appears R is replicating data when creating a time series where

> # requested date range exceeds available data.

>

> # The moral of the story is make sure there are no gaps in data

> # prior to creating the time series!!!

>

> df = data.frame()

>

> x = 10; yr = 2016; mo = 01; df = rbind(df, data.frame(x, yr, mo))

> x = 12; yr = 2016; mo = 02; df = rbind(df, data.frame(x, yr, mo))

> x = 15; yr = 2016; mo = 03; df = rbind(df, data.frame(x, yr, mo))

> x = 19; yr = 2016; mo = 04; df = rbind(df, data.frame(x, yr, mo))

>

> x = 11; yr = 2016; mo = 05; df = rbind(df, data.frame(x, yr, mo))

> x = 13; yr = 2016; mo = 06; df = rbind(df, data.frame(x, yr, mo))

> x = 16; yr = 2016; mo = 07; df = rbind(df, data.frame(x, yr, mo))

> x = 20; yr = 2016; mo = 08; df = rbind(df, data.frame(x, yr, mo))

>

> x = 12; yr = 2016; mo = 09; df = rbind(df, data.frame(x, yr, mo))

> x = 14; yr = 2016; mo = 10; df = rbind(df, data.frame(x, yr, mo))

> x = 17; yr = 2016; mo = 11; df = rbind(df, data.frame(x, yr, mo))

> x = 21; yr = 2016; mo = 12; df = rbind(df, data.frame(x, yr, mo))

>

> x = 13; yr = 2017; mo = 01; df = rbind(df, data.frame(x, yr, mo))

> x = 15; yr = 2017; mo = 02; df = rbind(df, data.frame(x, yr, mo))

> x = 18; yr = 2017; mo = 03; df = rbind(df, data.frame(x, yr, mo))

> x = 22; yr = 2017; mo = 04; df = rbind(df, data.frame(x, yr, mo))

>

> print(df)

    x   yr mo

1  10 2016  1

2  12 2016  2

3  15 2016  3

4  19 2016  4

5  11 2016  5

6  13 2016  6

7  16 2016  7

8  20 2016  8

9  12 2016  9

10 14 2016 10

11 17 2016 11

12 21 2016 12

13 13 2017  1

14 15 2017  2

15 18 2017  3

16 22 2017  4

>

> # get first and last month and year

> min_yr = min(df$yr)

> min_mo= min(subset(df, yr==min_yr)$mo)

> max_yr = max(df$yr)

> max_mo= max(subset(df, yr==max_yr)$mo)

>

> print(paste0("Have values from ", min_mo, "/", min_yr, " thru ", max_mo, "/", max_yr))

[1] "Have values from 1/2016 thru 4/2017"

>

> ts = ts(df$x, start=c(min_yr, min_mo), end=c(max_yr, max_mo), frequency=12)

> print(ts)

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

2016  10  12  15  19  11  13  16  20  12  14  17  21

2017  13  15  18  22                               

>

> max_mo = 8  # data does not really go out that far

> ts2 = ts(df$x, start=c(min_yr, min_mo), end=c(max_yr, max_mo), frequency=12)

> print(ts2)

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

2016  10  12  15  19  11  13  16  20  12  14  17  21

2017  13  15  18  22  10  12  15  19               

> # N O T E: I do not have data for May-Aug 2017 so it uses Jan-Apr 2015 ! ! !

>

> # drop one row, Oct 2017

> df = subset(df, !(yr == 2016 & mo == 10))

> print(df)

    x   yr mo

1  10 2016  1

2  12 2016  2

3  15 2016  3

4  19 2016  4

5  11 2016  5

6  13 2016  6

7  16 2016  7

8  20 2016  8

9  12 2016  9

11 17 2016 11

12 21 2016 12

13 13 2017  1

14 15 2017  2

15 18 2017  3

16 22 2017  4

> max_mo = 4  # undo prior test

> ts3 = ts(df$x, start=c(min_yr, min_mo), end=c(max_yr, max_mo), frequency=12)

> print(ts3)

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

2016  10  12  15  19  11  13  16  20  12  17  21  13

2017  15  18  22  10                               

> # N O T E: This shows df dates are ignored when creating time series ! ! !


Friday, May 4, 2018

R: min and max month and year



R: min and max month and year

  # get first and last month and year
  min_year = min(df$year)
  min_month= min(subset(df, year==min_year)$month)
  max_year = max(df$year)
  max_month= max(subset(df, year==max_year)$month)





Monday, April 2, 2018

SAS: List files in directory




I will just leave this here so I can find it later...


* which data files are available?
* macro copied from http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n0js70lrkxo6uvn1fl4a5aafnlgt.htm&docsetVersion=9.4&locale=en;
* with BQ mods to write file list to a sas table ;

%macro list_files(dir,ext);
  %local filrf rc did memcnt name i;
  %let rc=%sysfunc(filename(filrf,&dir));
  %let did=%sysfunc(dopen(&filrf));     

   %if &did eq 0 %then %do;
    %put Directory &dir cannot be open or does not exist;
    %return;
  %end;

   data work.&ext._files;
   attrib folder  length=$128;
   attrib filename length=$64;

   %do i = 1 %to %sysfunc(dnum(&did));  

   %let name=%qsysfunc(dread(&did,&i));

      %if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
        %put &dir\&name;

           folder = "&dir";
           filename = "&name";
           output;


      %end;

      %else %if %qscan(&name,2,.) = %then %do;       

        %list_files(&dir\&name,&ext)

      %end;

   %end;

   run;

   %let rc=%sysfunc(dclose(&did));
   %let rc=%sysfunc(filename(filrf));    

%mend list_files;

%list_files(&RAWDATA_FOLDER, xlsx);
%list_files(&RAWDATA_FOLDER, txt);
%list_files(&RAWDATA_FOLDER, dat);


Friday, February 16, 2018

SAS: Directory separator character



SAS: Directory separator character


%* ------------------------------------ ;
%* Set Directory Separator Character    ;
%* ------------------------------------ ;
       
%if ( %upcase(&SYSSCP.) = WIN ) %then %do;
    %let WHICH_SLASH = \ ;
%end;

%else %do;
    %let WHICH_SLASH = / ;

%end;







Friday, February 9, 2018

SAS: Delete all work files without having to list them



SAS: Delete all work files without having to list them


proc datasets library=work kill nolist;
run;