Date: Wed, 2 Aug 2000 16:58:11 +0100
Reply-To: Peter Crawford <peter.crawford@DB.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Peter Crawford <peter.crawford@DB.COM>
Subject: Re: iterate through all vars in a dataset
Content-type: text/plain; charset=us-ascii
now this description sounds much more like you don't want array
handling, but need my generalised "output to .csv" routine
%let dlm = '09'x ;
%let fromDS = your.data;
In sql the details for the column headers (quoted variable names)
are created in a macro variable
%let lib=%upcase(&fromDS);
%let mem=%scan( &lib, 2, . );
%let lib=%scan( &lib, 1, . );
proc sql noprint;
select quote(name) into :heads separated by "&dlm"
from dictionary.columns
where libname="&lib" and memname ="&mem" ;
QUIT;
In the data step these headers are output on the first iteration.
data _null_;
file "your.csv" ls=3000;
if _n_=1 then put &heads ;
set &fromDS;
format _character_ $quote200.;
put (_all_)( +(-1) &dlm ) @1 ' ';
run;
Table details are output with (_all_)(&dlm)
This default layout could probably extend to put cell tags by
simply extending the delimiters and customising the start
and end of row
Datum: 02/08/2000 15:10
An: Peter Crawford/Zentrale/DeuBaExt
SAS-L@listserv.uga.edu
millerj@calib.com
JackHamilton@firsthealth.com
jont@yebo.co.za
paul_dorfman@hotmail.com
J.Groeneveld@itgroups.com
Betreff: RE: iterate through all vars in a dataset
Nachrichtentext:
Peter (and the others who responded),
The names/values will be used to write the HTML for a web page based on
the contents of this dataset. (I'm aware of %DS2HTM, but the people I'm
helping with this project don't want to use it because they want the SAS
program to write some Javascript and/or ASP code mixed in with the HTML. I'd
rather just use %DS2HTM, but unfortunately it's not my decision.) So, the
names would be used for column headers in a table and the values would be
used as the data in the table rows.
I neglected to mention in my original question that the dataset will
contain both numeric and character variables, so I think this means I can't
use an array-based solution like the one I tried, since I believe arrays
have to be of all the same data type. Right now I'm inclined to try to use
a couple of 'pseudo-arrays' of macro variables to get around this.
Alternately, I could somehow convert all the numeric variables in the
dataset to character variables, then use an array solution.
Thanks for your response, and thanks also to Jeremy Miller, Jack Hamilton,
Jon Tugman, Paul Dorfman, and Jim Groeneveld for their suggestions. I'm
still working through these, but I think the solution I need is among them
(probably more than once!).
Sterling Price
> -----Original Message-----
> From: Peter Crawford [SMTP:peter.crawford@db.com]
> Sent: Wednesday, August 02, 2000 5:30 AM
> To: ssprice@wal-mart.com
> Cc: SAS-L@listserv.uga.edu
> Subject: RE: iterate through all vars in a dataset
>
>
> I can't imagine why you haven't used put _all_;
> This matches the style of the output your candidate routine is
> producing. It adds _n_ and _error_, but is very much simpler !
> If we knew how you wanted to use the name/value pairs it might
> be easier to suggest macro based solutuions like demo-ed in the
> on-line help for call set.
>
>
>
> Datum: 01/08/2000 22:25
> An: SAS-L@listserv.uga.edu
>
>
>
>
> Antwort an: ssprice@wal-mart.com
>
> Betreff: iterate through all vars in a dataset
> Nachrichtentext:
>
>
> Hi SAS-L'ers,
>
> I'm needing to iterate through all the variables in a SAS dataset, picking
> up both the name and value for each variable. Below is the (very flawed)
> approach I tried using some made-up data for simplicity. Can someone
> suggest a way to make this work, or is there (as I suspect) a better way
> to
> loop through a dataset and pick up these names/values? I don't
> necessarily
> have to load the names/values into arrays as shown below - if I can access
> them directly, so much the better.
>
> If it makes a difference, this is on v6.12 running under NT4.0
>
> Thanks for your time and any suggestions you have.
>
> Sterling Price
>
> data work.test;
> input name $ rank $ serial;
> cards;
> Joe Colonel 12345
> Bill Private 56789
> Ed Sergeant 76543
> ;
>
> proc sql noprint;
>
> select count(*) into :numobs
> from work.test;
>
> select nvar into :numvars
> from dictionary.tables
> where libname='WORK' and memname='TEST' and memtype='DATA';
>
> quit;
>
> data results(drop = i rc dsid);
>
> array names(&numvars) $ _temporary_;
> array vals(&numvars) $ _temporary_;
>
> dsid = open('WORK.TEST');
> rc=fetchobs(dsid,i);
> do i = 1 to &numvars;
>
> names(i) = varname(dsid,i); * load array with the names of the
> variables;
> varpos=varnum(dsid,names(i)); * position of this variable in the
> dataset;
> vals(i)=getvarc(dsid,varpos); * load array with the values of the
> variables;
>
> put names(i)= vals(i)=;
>
> end;
> run;
>
>
>
> **********************************************************************
> This email and any files transmitted with it are confidential
> and intended solely for the individual or entity to
> whom they are addressed. If you have received this email
> in error destroy it immediately.
> **********************************************************************
>
>
|