Advertisement
Guest User

Untitled

a guest
Mar 6th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SAS 20.41 KB | None | 0 0
  1. /*
  2.  _______           _        _______  _______          
  3. (  ____ )|\     /|( \      (       )(       )        
  4. | (    )|| )   ( || (      | () () || () () |        
  5. | (____)|| |   | || |      | || || || || || |        
  6. |  _____)( (   ) )| |      | |(_)| || |(_)| |        
  7. | (       \ \_/ / | |      | |   | || |   | |        
  8. | )        \   /  | (____/\| )   ( || )   ( |        
  9. |/          \_/   (_______/|/     \||/     \|        
  10.                                                      
  11.  ______   _______ _________ _______                  
  12. (  __  \ (  ___  )\__   __/(  ___  )                  
  13. | (  \  )| (   ) |   ) (   | (   ) |                  
  14. | |   ) || (___) |   | |   | (___) |                  
  15. | |   | ||  ___  |   | |   |  ___  |                  
  16. | |   ) || (   ) |   | |   | (   ) |                  
  17. | (__/  )| )   ( |   | |   | )   ( |                  
  18. (______/ |/     \|   )_(   |/     \|                  
  19.                                                      
  20. _________ _______  _______  _______  _______ _________
  21. \__   __/(       )(  ____ )(  ___  )(  ____ )\__   __/
  22.    ) (   | () () || (    )|| (   ) || (    )|   ) (  
  23.    | |   | || || || (____)|| |   | || (____)|   | |  
  24.    | |   | |(_)| ||  _____)| |   | ||     __)   | |  
  25.    | |   | |   | || (      | |   | || (\ (      | |  
  26. ___) (___| )   ( || )      | (___) || ) \ \__   | |  
  27. \_______/|/     \||/       (_______)|/   \__/   )_(  
  28.                                                      
  29. */
  30.  
  31. Options Symbolgen;
  32. /*  Declare temporary library   */
  33. %global tmplib;
  34. %let tmplib=work;
  35.  
  36. /*  Macro for initializing empty datasets   */
  37. %macro init(dset=);
  38. proc sql;
  39.     drop table &dset.;
  40. quit;
  41.  
  42. data &dset.;
  43. length Indicator $20
  44.     Reporting_country $4
  45.     reporting_country_label $40
  46.     'EU / WB'n $3
  47.     Week_Starting 8
  48.     YEAR 8
  49.     Month 8
  50.     Week 8
  51.     nat_orig $10
  52.     Nationality_pers $10
  53.     nat_pers_label $40
  54.     Returning_country $10
  55.     returning_country_label $40
  56.     Ctry_return $10
  57.     Failed_Asy $4
  58.     Criminal_Sanction $4
  59.     FacSus $4
  60.     ProtReason $20
  61.     Bus 8
  62.     Van 8
  63.     Car 8
  64.     Airplane 8
  65.     Other 8
  66.     NA 8
  67.     Region $40
  68.     Total 8
  69.     Forced 8
  70.     Voluntary 8
  71.     Stolen 8
  72.     Lost 8
  73.     PassengerType $10
  74.     BorderSection $10
  75.     'Entry/Exit'n $6
  76.     Return_operation $16
  77.     file $40
  78. ;
  79. Indicator="QQQQQQQQQQQQQQQQQQQ";
  80. Reporting_country="QQQQ";
  81. reporting_country_label="QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ";
  82. 'EU / WB'n="QQQ";
  83. nat_orig="QQQQ";
  84. Nationality_pers="QQQQQQQQQQ";
  85. nat_pers_label="QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ";
  86. Returning_country="QQQQQQQQQQ";
  87. returning_country_label="QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ";
  88. Ctry_return="QQQQQQQQQQ";
  89. Failed_Asy="QQQQ";
  90. Criminal_Sanction="QQQQ";
  91. FacSUS="QQQQ";
  92. ProtReason="QQQQQQQQQQQQQQQQQQQQ";
  93. Region="QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ";
  94. PassengerType="QQQQQQQQQQ";
  95. BorderSection="QQQQQQQQQQ";
  96. 'Entry/Exit'n="QQQQQQ";
  97. Return_operation="QQQQQQQQQQQQQQQQ";
  98. file="QQQQQQQQQQQQQQQQQQQQ";
  99.  
  100. format Indicator $20.
  101.     Reporting_country $20.
  102.     reporting_country_label $40.
  103.     'EU / WB'n $3.
  104.     Week_Starting DATE9.
  105.     YEAR bestd5.
  106.     Month bestd5.
  107.     Week bestd5.
  108.     nat_orig $4.
  109.     Nationality_pers $4.
  110.     nat_pers_label $40.
  111.     Returning_country $4.
  112.     returning_country_label $40.
  113.     Ctry_return $10.
  114.     Failed_Asy $4.
  115.     Criminal_Sanction $4.
  116.     FacSus $4.
  117.     ProtReason $20.
  118.     Bus bestd5.
  119.     Van bestd5.
  120.     Car bestd5.
  121.     Airplane bestd5.
  122.     Other bestd5.
  123.     NA bestd5.
  124.     Region $40.
  125.     Total bestd14.
  126.     Forced bestd14.
  127.     Voluntary bestd14.
  128.     Stolen bestd14.
  129.     Lost bestd14.
  130.     PassengerType $10.
  131.     BorderSection $10.
  132.     'Entry/Exit'n $6.
  133.     Return_operation $16.
  134.     file $40.
  135. ;
  136. run;
  137. %mend init;
  138.  
  139. proc sql noprint;
  140.     drop table &tmplib..PVLMM_Data;
  141. quit;
  142.  
  143. %init(dset=&tmplib..PVLMM_Data);
  144.  
  145. /*  Insert basic information    */
  146. %macro basic_input(dset=);
  147.     proc sql noprint;
  148.         /*  Country ISOCode */
  149.         select country
  150.         into :ctry
  151.         from &tmplib..ctry;
  152.     quit;
  153.     /*  Insert data */
  154.     data &dset.;
  155.     set &dset.;
  156.         length country $4 file $40;
  157.         Reporting_country="&ctry";
  158.         file="&file";
  159.     run;
  160.  
  161. %mend basic_input;
  162. /*  Normalize nationalities */
  163. %macro norm_nat(in=);
  164.     data &in.(rename=(nat2=nat_orig));
  165.     set &in.;
  166.         nat2=strip(upcase(nat_orig));
  167.         drop nat_orig;
  168.     run;
  169. %mend norm_nat;
  170. /*  Macro for reading Excel tabs    */
  171. %macro Import_One(file=);
  172.     %put File for import: &file.;
  173.    
  174.     proc sql noprint;
  175.         drop table &tmplib..ctry;
  176.         drop table &tmplib..ASY1;
  177.         drop table &tmplib..RET1_1;
  178.         drop table &tmplib..RET1_2;
  179.         drop table &tmplib..IBC1AOut;
  180.         drop table &tmplib..Passport;
  181.         drop table &tmplib..PassFlow;
  182.     quit;
  183.  
  184.     data &tmplib..ctry;
  185.         length country $4 file $40;
  186.         file="&file";
  187.         country=scan(file,1,"-");
  188.         /*
  189.         year=scan(file,3,"-")*1;
  190.         */
  191.         week_start=scan(file,4,"-")*1;
  192.         week_end=scan(scan(file,5,"-"),1,".")*1;
  193.     run;
  194.  
  195.     /*  Year    */
  196.     /*
  197.     proc import out=&tmplib..year
  198.             datafile="&root.\&file" replace
  199.             dbms=excelcs;
  200.         range="Instr & Def$C26:C26";
  201.         usedate=yes;
  202.         scantime=yes;
  203.     run;
  204.  
  205.     proc contents data=&tmplib..year out=&tmplib..year2(keep=name) noprint;
  206.     run;
  207.  
  208.     proc sql noprint;
  209.     select name
  210.     into :yrtmp
  211.     from &tmplib..year2;
  212.     quit;
  213.  
  214.     %global year;
  215.     %let year=&yrtmp;*/
  216.     /*  Asylum Inland   */
  217.     proc import out=&tmplib..ASY1
  218.             datafile="&root.\&file" replace
  219.             dbms=excelcs;
  220.         range="1 Asylum Inland$A8:K300";
  221.         usedate=yes;
  222.         scantime=yes;
  223.     run;
  224.  
  225.     /*  Return  */
  226.     proc import out=&tmplib..RET1_1
  227.             datafile="&root.\&file" replace
  228.             dbms=excelcs;
  229.         range="2 Return$A8:I300";
  230.         usedate=yes;
  231.         scantime=yes;
  232.     run;
  233.  
  234.     proc import out=&tmplib..RET1_2
  235.             datafile="&root.\&file" replace
  236.             dbms=excelcs;
  237.         range="1 Return$A8:I300";
  238.         usedate=yes;
  239.         scantime=yes;
  240.     run;
  241.  
  242.     /*  1A Out BCP  */
  243.     proc import out=&tmplib..IBC1AOUT
  244.             datafile="&root.\&file" replace
  245.             dbms=excelcs;
  246.         range="1A Out BCP$A8:F300";
  247.         usedate=yes;
  248.         scantime=yes;
  249.     run;
  250.  
  251.     /*  Passport    */
  252.     proc import out=&tmplib..PASSPORT
  253.             datafile="&root.\&file" replace
  254.             dbms=excelcs;
  255.         range="3 Passport$A8:F300";
  256.         usedate=yes;
  257.         scantime=yes;
  258.     run;
  259.  
  260.     /*  Passenger flow  */
  261.     proc import out=&tmplib..PASSFLOW
  262.             datafile="&root.\&file" replace
  263.             dbms=excelcs;
  264.         range="2 Passenger flow$A8:E300";
  265.         usedate=yes;
  266.         scantime=yes;
  267.     run;
  268.  
  269.     /*  Check if tables exist   */
  270.     proc sql noprint;
  271.         select memname
  272.         into :test1
  273.         from sashelp.vmember
  274.         where strip(upcase(memname))="ASY1";
  275.  
  276.         select memname
  277.         into :test2
  278.         from sashelp.vmember
  279.         where strip(upcase(memname))="RET1_1";
  280.  
  281.         select memname
  282.         into :test3
  283.         from sashelp.vmember
  284.         where strip(upcase(memname))="RET1_2";
  285.  
  286.         select memname
  287.         into :test4
  288.         from sashelp.vmember
  289.         where strip(upcase(memname))="IBC1AOUT";
  290.  
  291.         select memname
  292.         into :test5
  293.         from sashelp.vmember
  294.         where strip(upcase(memname))="PASSPORT";
  295.  
  296.         select memname
  297.         into :test6
  298.         from sashelp.vmember
  299.         where strip(upcase(memname))="PASSFLOW";
  300.     quit;
  301.     /*  If Asylum tab exists then proceed   */
  302.     %put &test1.;
  303.     %if %symexist(test1) %then %do;
  304.         data &tmplib..ASY2;
  305.         set &tmplib..ASY1;
  306.             where Totals*1 ne 0 and Totals*1 ne .;
  307.             rename Nationality=nat_orig
  308.                 'Suspected Facilitation'n=FacSus
  309.                 'Stated Reason for Protection'n=ProtReas;
  310.             Total=Totals*1;
  311.             drop Totals Bus Van Car Airplane Other 'N.A.'n;
  312.             length Indicator $20;
  313.             Indicator="Asylum Inland";
  314.             Bus1=Bus*1;
  315.             Van1=Van*1;
  316.             Car1=Car*1;
  317.             Airplane1=Airplane*1;
  318.             Other1=Other*1;
  319.             NA='N.A.'n*1;
  320.         run;
  321.  
  322.         data &tmplib..ASY2;
  323.         set &tmplib..ASY2;
  324.         rename Bus1=Bus Van1=Van Car1=Car Airplane1=Airplane Other1=Other;
  325.         run;
  326.  
  327.         %basic_input(dset=&tmplib..ASY2);
  328.         %norm_nat(in=&tmplib..ASY2);
  329.     %end;
  330.     /*  If Return tab exists then proceed   */
  331.     %put &test2.;
  332.     %if %symexist(test2) %then %do;
  333.         data &tmplib..RET2_1;
  334.         set &tmplib..RET1_1;
  335.             where 'Total Returns'n*1 ne 0 and 'Total Returns'n*1 ne .;
  336.             rename Nationality=nat_orig
  337.                 'Country of Return'n=Ctry_return
  338.                 'Returning Country'n=returning_country_label
  339.                 'Negative Asylum _Decision'n=Failed_Asy
  340.                 'Criminal Sanctions /_Extradition'n=Criminal_Sanction;
  341.             Total='Total Returns'n*1;
  342.             Forced='Total Forced'n*1;
  343.             Voluntary='Total Voluntary'n*1;
  344.             length Indicator $20;
  345.             if F9 eq "refusal" then Indicator="Refusal";
  346.             else Indicator="Return MS";
  347.             drop 'Total Returns'n 'Total Forced'n 'Total Voluntary'n F9;
  348.         run;
  349.  
  350.         %basic_input(dset=&tmplib..RET2_1);
  351.         %norm_nat(in=&tmplib..RET2_1);
  352.  
  353.         data &tmplib..RET2F(drop=Total Voluntary) &tmplib..RET2V(drop=Total Forced);
  354.         set &tmplib..RET2_1;
  355.             where Indicator ne "Refusal";
  356.         run;
  357.  
  358.         data &tmplib..RET2R(drop=Voluntary Forced)
  359.         set &tmplib..RET2_1;
  360.             where Indicator eq "Refusal" and Total ne 0;
  361.         run;
  362.  
  363.         data &tmplib..RET2F;
  364.         set &tmplib..RET2F;
  365.             Return_operation="Forced";
  366.             where Forced ne 0;
  367.             rename Forced=Total;
  368.         run;
  369.  
  370.         data &tmplib..RET2V;
  371.         set &tmplib..RET2V;
  372.             Return_operation="Voluntary";
  373.             where Voluntary ne 0;
  374.             rename Voluntary=Total;
  375.         run;
  376.        
  377.         %init(dset=&tmplib..RET2);
  378.  
  379.         data &tmplib..RET2;
  380.             if 0 then modify &tmplib..RET2;
  381.             set &tmplib..RET2F open=defer;
  382.             output;
  383.         run;
  384.  
  385.         data &tmplib..RET2;
  386.             if 0 then modify &tmplib..RET2;
  387.             set &tmplib..RET2V open=defer;
  388.             output;
  389.         run;
  390.  
  391.         data &tmplib..RET2;
  392.             if 0 then modify &tmplib..RET2;
  393.             set &tmplib..RET2R open=defer;
  394.             output;
  395.         run;
  396.  
  397.         data &tmplib..RET2;
  398.         set &tmplib..RET2;
  399.             where Total ne . and Total ne 0;
  400.         run;
  401.         /*
  402.         proc append base=&tmplib..RET2 data=&tmplib..RET2F;
  403.         run;
  404.  
  405.         proc append base=&tmplib..RET2 data=&tmplib..RET2V;
  406.         run;
  407.  
  408.         proc append base=&tmplib..RET2 data=&tmplib..RET2R;
  409.         run;
  410.         */
  411.     %end;
  412.  
  413.     %put &test3.;
  414.     %if %symexist(test3) %then %do;
  415.         data &tmplib..RET2;
  416.         set &tmplib..RET1_2;
  417.             where 'Total Returns'n*1 ne 0 and 'Total Returns'n*1 ne .;
  418.             rename Nationality=nat_orig
  419.                 'Country of Return'n=Ctry_return
  420.                 'Returning Country'n=returning_country_label
  421.                 'Negative Asylum _Decision'n=Failed_Asy
  422.                 'Criminal Sanctions /_Extradition'n=Criminal_Sanction;
  423.             Total='Total Returns'n*1;
  424.             Forced='Total Forced'n*1;
  425.             Voluntary='Total Voluntary'n*1;
  426.             length Indicator $20;
  427.             if F9="refusal" then Indicator="Refusal";
  428.             else Indicator="Return MS";
  429.             drop 'Total Returns'n 'Total Forced'n 'Total Voluntary'n F9;
  430.         run;
  431.  
  432.         %basic_input(dset=&tmplib..RET2);
  433.         %norm_nat(in=&tmplib..RET2);
  434.     %end;
  435.     /* If 1A Out BCP tab exists then proceed */
  436.     %put &test4.;
  437.     %if %symexist(test4) %then %do;
  438.         data &tmplib..IBC1AOut2;
  439.         set &tmplib..IBC1AOut;
  440.             where Totals*1 ne 0 and Totals*1 ne .;
  441.             rename Nationality=nat_orig;
  442.             Total=Totals*1;
  443.             length Indicator $20;
  444.             Indicator="ibc-1a";
  445.             keep Week_Starting Nationality nat_orig Total Indicator;
  446.         run;
  447.  
  448.         %basic_input(dset=&tmplib..IBC1AOut2);
  449.         %norm_nat(in=&tmplib..IBC1AOut2);  
  450.     %end;
  451.     /*  If Passport tab exists then proceed */
  452.     %put &test5.;
  453.     %if %symexist(test5) %then %do;
  454.         data &tmplib..Passport2;
  455.         set &tmplib..Passport;
  456.             length Indicator $20;
  457.             rename Nationality=nat_orig 'Administrative region'n=Region;
  458.             Total='Total new requests'n*1;
  459.             Lostn=lost*1;
  460.             Stolenn=stolen*1;
  461.             Indicator="Passport";
  462.             where Week_Starting ne .;
  463.             keep Week_Starting Nationality nat_orig Total Indicator 'Administrative region'n Region Lostn Stolenn;
  464.         run;
  465.  
  466.         data &tmplib..Passport3;
  467.         set &tmplib..Passport2;
  468.             where (Total ne 0
  469.             or Total ne .
  470.             or Lostn ne 0
  471.             or Lostn ne .
  472.             or Stolenn ne 0
  473.             or Stolenn ne .) and
  474.             Week_Starting ne .;
  475.             rename Lostn=Lost Stolenn=Stolen;
  476.         run;
  477.  
  478.         %basic_input(dset=&tmplib..Passport3);
  479.         %norm_nat(in=&tmplib..Passport3);
  480.     %end;
  481.     /*  If PassFlow tab exists then proceed */
  482.     %put &test6.;  
  483.     %if %symexist(test6) %then %do;
  484.         data &tmplib..PASSFLOW2;
  485.         set &tmplib..PASSFLOW;
  486.             length Indicator $20;
  487.             where 'Totals'n*1 ne 0 and 'Totals'n*1 ne .;
  488.             rename 'Type of passangers'n=PassengerType 'Border section'n=BorderSection;
  489.             Total=Totals*1;
  490.             Indicator="Passenger flow";
  491.             keep Indicator Week_Starting PassengerType Total BorderSection 'Entry/Exit'n;
  492.         run;
  493.  
  494.         %basic_input(dset=&tmplib..PassFlow2);
  495.     %end;
  496.     /*  Append tables   */
  497.     data &tmplib..PVLMM_Data;
  498.         if 0 then modify &tmplib..PVLMM_Data;
  499.         set &tmplib..ASY2 open=defer;
  500.         output;
  501.     run;
  502.  
  503.     data &tmplib..PVLMM_Data;
  504.         if 0 then modify &tmplib..PVLMM_Data;
  505.         set &tmplib..RET2 open=defer;
  506.         output;
  507.     run;
  508.  
  509.     data &tmplib..PVLMM_Data;
  510.         if 0 then modify &tmplib..PVLMM_Data;
  511.         set &tmplib..IBC1AOut2 open=defer;
  512.         output;
  513.     run;
  514.  
  515.     data &tmplib..PVLMM_Data;
  516.         if 0 then modify &tmplib..PVLMM_Data;
  517.         set &tmplib..Passport3 open=defer;
  518.         output;
  519.     run;
  520.  
  521.     data &tmplib..PVLMM_Data;
  522.         if 0 then modify &tmplib..PVLMM_Data;
  523.         set &tmplib..PassFlow2 open=defer;
  524.         output;
  525.     run;
  526. %mend Import_One;
  527.  
  528. proc sql;
  529.     drop table &tmplib..PVLMM_Data3;
  530.     drop table &tmplib..PVLMM_Data4;
  531.     drop table &tmplib..Base_countries;
  532.     drop table &tmplib..PVLMM_Data5;
  533.     drop table &tmplib..nat_dictionary;
  534.     drop table &tmplib..PVLMM_Data6;
  535.     drop table &tmplib..nat_labels;
  536.     drop table &tmplib..PVLMM_Data7;
  537.     drop table &tmplib..PVLMM_Data_Final;
  538.     drop table &tmplib..PVLMM_Data_Final2;
  539.     drop table &tmplib..PVLMM_Data_Final3;
  540. quit;
  541.  
  542. /*  Loop for importing all existing files in a folder   */
  543. %macro ImportAllFiles;
  544.     /*  Path to main folder */
  545.     %global path1;
  546.     %let path1 = \\aofrontex.local\frontex-shared\Restricted Area\Operations Division\RAU\RAU Work\FRAN\REPORTING\TOOLS\PVLMM\&year.;
  547.     %global root;
  548.     %let root=&path1.\&path2;
  549.     %put Chosen path: &root.;
  550.    
  551.     filename exdir "&root.";
  552.     %let dirid = %sysfunc(DOPEN(exdir));
  553.     filename exdir clear;
  554.     %put &dirid.;
  555.     %let dircnt = %sysfunc(DNUM(&dirid));
  556.     %put &dircnt.;
  557.     /*  Loop over all files in chosen folder    */
  558.     %do i = 1 %to &dircnt;
  559.         %let dirread = %sysfunc(DREAD(&dirid,&i));
  560.         /*  Process only WBMM files */
  561.         %local regexid;
  562.         %let regexid1=%sysfunc(prxmatch("WBMM","&dirread"));
  563.         %let regexid2=%sysfunc(prxmatch("PVLMM","&dirread"));
  564.         %if &regexid1>0 %then %do;
  565.         %put Current file&dirread.;
  566.             /*  Macro for reading excel tabs    */
  567.             %Import_One(file=&dirread);
  568.         %end;
  569.         %if &regexid2>0 %then %do;
  570.         %put Current file&dirread.;
  571.             /*  Macro for reading excel tabs    */
  572.             %Import_One(file=&dirread);
  573.         %end;
  574.     %end;
  575.     %let rc =%sysfunc(DCLOSE(&dirid));
  576.     %put &rc.;
  577.    
  578. %mend ImportAllFiles;
  579.  
  580. /********************************************/
  581. /*  Choose year !!!!!!!!!!!!!!!!!!!!!!!!!!  */
  582. /********************************************/
  583. %global year;
  584. %let year=2014;
  585. /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  586. /*  Change the folder name here !!!!!!!!!   */
  587. /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  588. %global path2;
  589. %let path2=7 April 2014;
  590. /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  591.  
  592. %global path1;
  593. %let path1 = \\aofrontex.local\frontex-shared\Restricted Area\Operations Division\RAU\RAU Work\FRAN\REPORTING\TOOLS\PVLMM\&year.;
  594. %global root;
  595. %let root=&path1.\&path2;
  596.  
  597. *%Import_One(file=ALB-WBMM-2013-49-52-v1.xls);
  598.  
  599. %ImportAllFiles;
  600.  
  601. /*  All post-import operations  */
  602.  
  603. proc sql;
  604.     create table &tmplib..PVLMM_Data3 as
  605.     select distinct Indicator,Reporting_country,reporting_country_label,'EU / WB'n,Week_Starting,YEAR,Month,Week,nat_orig,Nationality_pers,nat_pers_label,Returning_country,returning_country_label,Ctry_return,Failed_Asy,Criminal_Sanction,FacSus,ProtReason,sum(Bus) as Bus,sum (Van) as Van,sum(Car) as Car,sum(Airplane) as Airplane,sum(Other) as Other,sum(NA) as NA,Region,sum(Total) as Total,Stolen,Lost,PassengerType,BorderSection,'Entry/Exit'n,Return_operation,file
  606.     from &tmplib..PVLMM_Data
  607.     group by Indicator,Reporting_country,reporting_country_label,'EU / WB'n,Week_Starting,YEAR,Month,Week,nat_orig,Nationality_pers,nat_pers_label,Returning_country,returning_country_label,Ctry_return,Failed_Asy,Criminal_Sanction,FacSus,ProtReason,Region,Stolen,Lost,PassengerType,BorderSection,'Entry/Exit'n,Return_operation,file;
  608. quit;
  609.  
  610. data &tmplib..PVLMM_Data4;
  611. set &tmplib..PVLMM_Data3;
  612.     where Indicator ne "QQQQQQQQQQQQQQQQQQQ";
  613.     if Reporting_country="GER" then Reporting_country="DEU";
  614.     if Reporting_country="GER" then Reporting_country_label="Germany";
  615.     if Reporting_country="DEU" then Reporting_country_label="Germany";
  616.     if Nationality_pers="" and Indicator ne "" then do;
  617.         Nationality_pers="UNK";
  618.         nat_pers_label="Unknown";
  619.     end;
  620. run;
  621.  
  622. /********************************************/
  623. /*  Import Isocode dictionary - BruteForce  */
  624. /********************************************/
  625.  
  626. %put ________Import Isocode dictionary________;
  627.  
  628. %put ________Import Isocode dictionary Finished________;
  629.  
  630. proc sql;
  631. create table &tmplib..PVLMM_Data5 as
  632. select * from &tmplib..PVLMM_Data4
  633. left outer join base_countries
  634. on PVLMM_Data4.Reporting_country=base_countries.iso3code;
  635. quit;
  636.  
  637. /*  Create Nationality_pers based upon Nat_orig - check for errors  */
  638. /*  Path to Excel with list of corrected nationality iso codes  */
  639.  
  640. %global path;
  641. %let path=\\aofrontex.local\frontex-shared\Restricted Area\Operations Division\RAU\RAU Work\FRAN\REPORTING\TOOLS\PVLMM;
  642. %global in_file;
  643. %let in_file=RANCompileMacro_afterJuly2013.xlsx;
  644.  
  645. proc import out=&tmplib..nat_dictionary(rename=(Coor=nat_orig UNK=nat_dest)) replace
  646.             datafile="&path.\&in_file"
  647.             dbms=excelcs;
  648.         range="Corr_code$A2:B800";
  649.         usedate=yes;
  650.         scantime=yes;
  651. run;
  652.  
  653. libname shredstg '\\aofrontex.local\sas-shareddrive\SASData';
  654.  
  655. proc sql noprint;
  656.     create table &tmplib..PVLMM_Data6 as
  657.     select * from &tmplib..PVLMM_Data5 as t1
  658.     left outer join &tmplib..nat_dictionary as t2
  659.     on t1.nat_orig=t2.nat_orig;
  660.  
  661.     create table &tmplib..nat_labels as
  662.     select distinct nationality_pers, nat_pers_label as temp
  663.     from shredstg.fran_analysis;
  664. quit;
  665.  
  666. data &tmplib..PVLMM_Data7;
  667. set &tmplib..PVLMM_Data6;
  668.     reporting_country_label=name;
  669.     nationality_pers=nat_dest;
  670.     if eu=1 or schengen=1 then do;
  671.         'EU / WB'n='MS';
  672.     end;
  673.     else do;
  674.         'EU / WB'n='WB';
  675.         if Indicator='Return MS' then Indicator='Return WB';
  676.     end;
  677.     drop id name capname frename capfrename iso2code iso3code eu regionid subregionid visa regionname subregionname schengen nat_dest;
  678. run;
  679.  
  680. proc sql noprint;
  681.     create table &tmplib..PVLMM_Data_Final as
  682.     select * from &tmplib..PVLMM_Data7 as t1
  683.     left outer join &tmplib..nat_labels as t2
  684.     on t1.nationality_pers=t2.nationality_pers;
  685.  
  686.     create table &tmplib..PVLMM_Data_Final2 as
  687.     select * from &tmplib..PVLMM_Data_Final
  688.     left outer join base_countries
  689.     on PVLMM_Data_Final.returning_country_label=base_countries.name;
  690. quit;
  691.  
  692. data &tmplib..PVLMM_Data_Final3;
  693. set &tmplib..PVLMM_Data_Final2;
  694.     format Week_Starting date9.;
  695.     returning_country=iso3code;
  696.     nat_pers_label=temp;
  697.    
  698.     drop temp drop id name capname frename capfrename iso2code iso3code eu regionid subregionid visa regionname subregionname schengen;
  699.     Week=week(Week_Starting,'v');
  700.     if week(Week_Starting,'v')<10 then do;
  701.         YYYYWK=strip(strip(year(Week_Starting))||'Wk '||strip(week(Week_Starting,'v')));
  702.     end;
  703.     else do;
  704.         YYYYWK=strip(strip(year(Week_Starting))||'Wk'||strip(week(Week_Starting,'v')));
  705.     end;
  706.     if month(Week_Starting)<10 then do;
  707.         YYYYMM=strip(strip(year(Week_Starting))||'M '||strip(month(Week_Starting)));
  708.     end;
  709.     else do;
  710.         YYYYMM=strip(strip(year(Week_Starting))||'M'||strip(month(Week_Starting)));
  711.     end;
  712.     Month=month(Week_Starting);
  713.     Year=year(Week_Starting);
  714.  
  715.     if month(Week_Starting)<4 then YYYYQ=cats(Year,'Q1');
  716.     if month(Week_Starting)>=4 and Month<7 then YYYYQ=cats(Year,'Q2');
  717.     if month(Week_Starting)>=7 and Month<10 then YYYYQ=cats(Year,'Q3');
  718.     if month(Week_Starting)>=10 then YYYYQ=cats(Year,'Q4');
  719.  
  720.     where Total ne 0 and Total ne .;
  721.  
  722.     if month=12 and week=1 then YYYYWK=strip(strip(year(Week_Starting)+1)||'Wk'||strip(week(Week_Starting,'v')));
  723. run;
  724. /*
  725. proc sort data=&tmplib..PVLMM_Data_Final3 out=&tmplib..PVLMM_Data_Final4 noduprecs;
  726. by _ALL_;
  727. run;
  728. */
  729. /*  Update the table in Sample Shared Storage   */
  730. /*
  731. data _null_;
  732.     call symdel('tab_exist');
  733. run;
  734.  
  735. proc sql noprint;
  736.         select memname
  737.         into :tab_exist
  738.         from sashelp.vmember
  739.         where memname="PVLMM_TRIAL_2" and libname="SHREDSTG";
  740. quit;
  741.  
  742. %macro condition;
  743. %if %symexist(tab_exist) %then %do;
  744.     data shredstg.PVLMM_TRIAL_2;
  745.         if 0 then modify shredstg.PVLMM_TRIAL_2;
  746.         set &tmplib..PVLMM_Data_Final3 open=defer;
  747.         output;
  748.     run;
  749. %end;
  750. %else %do;
  751.     data shredstg.PVLMM_TRIAL_2;
  752.         set &tmplib..PVLMM_Data_Final3;
  753.     run;
  754. %end;
  755. %mend condition;
  756.  
  757. *%condition;*/
  758.  
  759.     data shredstg.PVLMM;
  760.         if 0 then modify shredstg.PVLMM;
  761.         set &tmplib..PVLMM_Data_Final3 open=defer;
  762.         output;
  763.     run;
  764.  
  765. /*
  766. proc append base=shredstg.PVLMM data=&tmplib..PVLMM_Data_Final3;
  767. run;
  768. /*
  769. proc sort data=shredstg.PVLMM_TRIAL_2 noduprecs;
  770. by _ALL_;
  771. run;
  772. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement