Monday, June 29, 2009

Output a dataset from proc contents!


proc contents data = check
out = check1(keep=name format label length name type varnum);
run;

Many of us use the procedure: proc contents but only few make use of the resulting dataset. The output dataset of proc contents helps the user visualize data items (in the form of a dataset). For example one can see the name, format, label, length, name, type, varnum etc.

Friday, June 26, 2009

Template for merging with SUPP's


%macro mer(seq, select);
data lb1;
set lb;
seq=&seq;
run;

data supp;
set supplb;
if strip(qnam)=&select;
seq=input(idvarval,best.);
run;

proc sort data=lb1; by usubjid seq;
proc sort data=supp; by usubjid seq;
data test;
merge lb1(in=a) supp(in=b);
by usubjid seq;
if a & b;
run;

proc sort data = test out = test1
dupout = dups nodupkey;
by studyid rdomain usubjid idvar idvarval qnam qlabel qval qorig qeval;
run;
%mend;

%mer(lbseq, 'CS');

Sunday, June 7, 2009

Conversion of character date & time to numeric


The following macro dtcn helps in conversion of character date and time to numeric date and time.

%macro dtcn(indt,dtmn);
length dt1 $10 tm1 $6 dt1n $20;
if index(&indt,'T') ne 0 then do;
dt1 = scan(&indt,1,'T');
tm1 = scan(&indt,2,'T');
if dt1 ne '' and tm1 ne '' then dt1n = put(input(dt1,yymmdd10.),date9.)' 'trim(left(tm1));
if dt1 ne '' and tm1 ne '' then &dtmn = input(dt1n,datetime15.);
else if dt1 ne '' and tm1 eq '' then &dtmn =input(put(input(dt1,yymmdd10.),date9.),date9.);
end;
else &dtmn = .;
%mend;


data date;
xxstdtc='2009-01-02T17:30';
run;

data date1;
format xxstdtn datetime15.;
set date;
%dtcn(xxstdtc, xxstdtn);
run;

Saturday, June 6, 2009

Find out sum


There are two possible ways to find out the sum of variable x.

data dummy;
input pt $1 seq x;
datalines;
1 1 6
1 2 1
1 3 .
2 1 4
3 1 .
4 1 .
4 2 2
5 1 3
6 1 .
7 1 3
;
run;
proc sort data=dummy; by pt seq; run;

1. One can either transpose the value of variable x (by identifier seq) & find the sum.

proc transpose data=dummy out=dummy1 prefix=_;
by pt;
id seq;
var x;
run;

data sumt;
set dummy1;
val=sum(_1,_2,_3);
run;

2. Or can use the following code to calculate it.

The code given below calculates the sum of variable x without transposing data:

data sum;
set dummy;
by pt;
retain val;
if first.pt then do;
*set the value to missing when it reads first.pt;
val = .;
*& finds where x is non-missing to fill the column val with first.pt value;
if x ne . then val = x;
end;
*does an: addition of two value & retain the same;
else if x ne . & val ne . then val = val+x;
*incase when first.pt is missing & other records of the same pt are available: then fills the column val with value of x;
else if x ne . & val eq . then val = x;
run;

data sum1;
set sum;
by pt;
if last.pt;
keep pt x val;
run;