LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (August 2009, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Mon, 10 Aug 2009 16:14:06 -0400
Reply-To:   Chang Chung <chang_y_chung@HOTMAIL.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   Chang Chung <chang_y_chung@HOTMAIL.COM>
Subject:   Re: Is there anyways to dynamically change datatypes for all variables?

OK. My regrets. Couple of typos are fixed. Ran on 9.2 on windows. Chang

/* test data */ data one; infile cards truncover; input (ind pat_id col1-col4) (:$20.); cards; AB 11 12.00 USA 29645 01/12/08 XY 12 Richmond 56 06/12/09 15 ; run;

data meta; input (ind col type) ($); cards; AB col1 AMT AB col2 CHAR AB col3 NUM AB col4 DATE XY col1 STR XY col2 NUM XY col3 DATE XY col4 NUM run;

/* type conversion meta data. parameterized code templates */ data types; infile cards truncover; input type $ 1-4 code $char100.; cards4; AMT [c] = input(old_[c], best.); format [c] dollar12.2; CHAR [c] = old_[c]; /* do nothing */ DATE [c] = input(old_[c], anydtdte.); format [c] mmddyys8.; NUM [c] = input(old_[c], best.); STR [c] = old_[c]; /* do nothing */ ;;;; run;

/* join meta and types to have one dataset, code */ proc sql; create table code as select ind, col, t.type, tranwrd(code, "[c]", strip(col)) as code from meta as m left join types as t on m.type = t.type order by ind, col; quit;

/* convert and output single ind */ %macro converted(ind=); %*-- subset one and convert, then output --*; data &ind; set one(rename=(col1=old_col1 col2=old_col2 col3=old_col3 col4=old_col4)); where ind="&ind"; drop old_:; %*-- get the conversion code into a macro --*; %local ds rc code; %let ds = %sysfunc(open(code(where=(ind="&ind")),in)); %syscall set(ds); %do %while(%sysfunc(fetch(&ds))=0); &code %end; %let rc = %sysfunc(close(&ds)); run; %mend converted;

/* do all -- assuming that meta has all ind^s */ proc freq data=meta noprint; tables ind/out=inds; run; data _null_; set inds; macro = cat('%converted(ind=', ind, ')'); call execute(macro); run;

/* check */ proc contents data=ab varnum; run; /* on lst, in part # Variable Type Len Format 1 ind Char 20 2 pat_id Char 20 3 col1 Num 8 DOLLAR12.2 4 col2 Char 20 5 col3 Num 8 6 col4 Num 8 MMDDYYS8. */ proc print data=ab; run; /* on lst Obs ind pat_id col1 col2 col3 col4 1 AB 11 $12.00 USA 29645 01/12/08 */ proc print data=xy; run; /* on lst Obs ind pat_id col1 col2 col3 col4 1 XY 12 Richmond 56 06/12/09 15 */


Back to: Top of message | Previous page | Main SAS-L page