Monday, October 22, 2012

Pick the latest code version by the version number

This question is from mitbbs. The original question is for CS.

 The question is: given a series of version number, how to pick the latest version code? For example, given "1.3.8", "1.23.2", "1.9.15", "2.1", "0.99.99", "2.0.5.89", how to pick the latest code? (it should be "2.1" here in the example).  In SAS, we can easily split the version number into cells(use array) and then fill up each cell with 0(use format) to compare.

 The code is like:


 
data a;
input a & $100.;
cards;
1.3.9
1.32.08
1.2.7
3.5.7.89
5
1.60.10
1.7
0.3
;
run;

proc print data=a;
run;

data b;
        set a;
        array a_split[*] a_1-a_10;
        do i=1 to dim(a_split);
                a_split[i]=scan(a,i,'.');
        end;
run;

data b;
        set b(drop=i);
        format _numeric_ z10.;
run;

proc sort data=b;
        by a_1--a_10;
run;

proc print data=b width=min;
run;



The final output is:

Thursday, October 11, 2012

Linux and SAS time set up

When kick off jobs for others, it requires to use first day and last day of last month as the parameter. If run sas script file in cron job and pick the time using linux command:

fst_day_this_mon=$(date -d "$date" +01%b%Y)
lst_day_last_mon=$(date -d "-1 day $fst_day_this_mon" +%d%b%Y)

echo fst_day_this_mon=$fst_day_this_mon  lst_day_last_mon=$lst_day_last_mon

In SAS we can use intnx function:

## in sas we can use function intnx with optional argument 'B' 'E'
## data _null_;
##      x=today();
##      stdt=intnx('month',x,-1,'B');
##      eddt=intnx('month',x,-1,'E');
##      call symput('stdt', put(stdt,date9.));
##      call symput('eddt', put(eddt,date9.));
## run;
##
## %put stdt=&stdt  eddt=&eddt;

Another similar code to pick the first day is:


d1=$(date -d "-1 month -$(($(date +%d)-1)) days" +"%d%b%Y")
d2=$(date -d "+13 day $d1" +"%d%b%Y")
d3=$(date -d "$d1+14days" +"%d%b%Y")
d4=$(date -d "$d1+27days" +"%d%b%Y")

echo "$d1" "$d2" "$d3" "$d4"

Wednesday, October 3, 2012

Rename all the variables in a data set


Some times it is necessary to rename all the variables. For example, it may be more convenient to use in array part.

An example to rename all the variables is:




data a;
input a b c d e f g h i j k l m n o p q;
cards;
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3
;
run;

proc contents data=a out=varnames;
run;

proc print data=varnames;
run;

filename pre "./pre.txt";

data _null_;
 file pre;
        set varnames end=eof;
 if _n_=1 then put "rename ";
        newvarname=trim(name)||'pre';
        put name '= ' newvarname;
 if eof then put ';';
run;

data b;
        set a;
        %include pre;
run;

proc print data=b;
run;