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 (July 2000, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Tue, 11 Jul 2000 11:04:09 -0700
Reply-To:     "Terjeson, Mark" <TERJEMW@DSHS.WA.GOV>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         "Terjeson, Mark" <TERJEMW@DSHS.WA.GOV>
Subject:      Re: Reformatting variables using metadata: need SQL and/or MACRO
              help
Comments: To: "haworthl@MINDSPRING.COM" <haworthl@MINDSPRING.COM>
Content-Type: text/plain

Hi Lauren, The CALL EXECUTE() allows you to build strings based on the contents of your incoming dataset and these lines are "stored" in the order created, and then at the end of the datastep these stored lines are submitted as if it was code you hand wrote. Your end result may need to be something other than the ATTRIB statement, but the idea here is that you can design what you need "on the fly" via the CALL EXECUTE().

data table1; varname='var1'; vartype='char'; varlen=10; vardec=.; output; varname='var2'; vartype='numb'; varlen=8 ; vardec=2; output; run;

data table2; input var1 $ var2; cards; Coffee 0.75 Tea 1.25 Latte 2.50 ; run;

* in order to accomplish something like this ; data table3; attrib var1 length=$10 format=$10.; attrib var2 length=8 format=8.2; set table2; run;

* you can have it built dynamically like this ; data _null_; length stmp $100; set table1 end=done; if _n_ eq 1 then do; call execute('data table3;'); end;

* for each row in table1, ; * create attrib statement ; stmp = ' attrib '||compress(varname)||' length='; if vartype eq 'char' then do; stmp = trim(stmp)||'$'||compress(put(varlen,4.)) ||' format=$'||compress(put(varlen,4.))||'.;'; end; if vartype eq 'numb' then do; stmp = trim(stmp)||compress(put(varlen,4.)) ||' format='||compress(put(varlen,4.)) ||'.'||compress(put(vardec,4.))||';'; end; call execute(stmp);

if done then do; call execute(' set table2;'); call execute('run;'); end; run;

Hope this is helpful, Mark Terjeson Washington State Department of Social and Health Services Division of Research and Data Analysis (RDA) (360) 902-0741 (360) 902-0705 fax mailto:terjemw@dshs.wa.gov

> -----Original Message----- > From: haworthl@MINDSPRING.COM [SMTP:haworthl@MINDSPRING.COM] > Sent: Tuesday, July 11, 2000 10:06 AM > To: SAS-L@LISTSERV.UGA.EDU > Subject: Reformatting variables using metadata: need SQL and/or MACRO > help > > Okay, I'm feeling lazy. My hope is that someone has done this before. > > My problem: 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? > > TIA, > > Lauren Haworth > > > Sent via Deja.com http://www.deja.com/ > Before you buy.


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