Date: Tue, 11 Jul 2000 20:38:20 GMT
Reply-To: sashole@mediaone.net
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Paul Dorfman <paul_dorfman@HOTMAIL.COM>
Subject: Re: Reformatting variables using metadata: need SQL and/or MACRO
help
Content-Type: text/plain; format=flowed
Lauren Haworth haworthl@MINDSPRING.COM wrote, in part:
>I have two datasets
>(1) metatdata including variable names, and their appropriate >formats;
>(2) raw data for these variables, all formatted as $200.
>I want to use the metadata to create a third dataset with the >variables
>all properly formatted.
>Some sample data:
>Dataset 1
>VARNAME VARTYPE VARLEN VARDEC
>var1 char 10
>var2 numb 8 2
>Dataset 2
>VAR1 VAR2
>Coffee 0.75
>Tea 1.25
>Latte 2.50
>What I want is a dataset 3 where VAR1 is formatted as $10. and VAR2 >is
>formatted as 8.2
>Any suggestions? I suspect this is a candidate for PROC SQL and/or a
>creative macro. Anyone have any code for something like this?
Lauren,
I assume you already have your 'raw' data in a SAS dataset, only all the
variables have been input as 200-byte strings. Unfortunately, nothing
elegant occurs to me at the moment. However, the crude solution below seems
to ne workable, its only limitation being the length <= 31 imposed on the
names of the variables.
proc format; value $typ 'char' = ' $' 'numb' = ' '; run;
proc sql;
select trim(varname)||'=z'||trim(varname)
into : rnmelist separated by ' ' from d1
where vartype eq 'numb'
;
select trim(varname)||'=input(compress(z'||trim(varname)||'),'||
trim(varlen )||'.'||trim(vardec)||')'
into : asgnlist separated by ';' from d1
where vartype = 'numb'
;
select trim(varname)||' '||put(vartype,$typ.)||
trim(varlen )||'.'||trim(vardec)
into : fmtlist separated by ' ' from d1
;
quit;
data d3 (drop=z:);
set d2 (rename=(&rnmelist));
&asgnlist;
informat &fmtlist;
format &fmtlist;
run;
Try to run it against a sample input, say
data d1;
infile cards missover;
input varname $ vartype $ varlen $ vardec $;
cards;
var1 char 10
var2 numb 8 2
var3 numb 20 8
var4 char 34
run;
data d2;
informat var1-var4 $200.;
format v: $200.;
input var1-var4;
cards;
coffee 0.75 12345678.91011234 abracadavra
tea 1.25 09876543.10192837 nonsensevar
latte 2.50 19203847.64510383 delirium
run;
The contents of the 3rd dataset should be like the following:
# Variable Type Len Pos Format Informat
---------------------------------------------------------
1 var1 Char 200 16 $10. $10.
3 var2 Num 8 0 8.2 8.2
4 var3 Num 8 8 20.8 20.8
2 var4 Char 200 216 $34. $34.
---------------------------------------------------------
Of course, the informat statement can be omitted if not needed.
Kind regards,
====================
Paul M. Dorfman
Jacksonville, Fl
====================
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com