Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

specify dataset variable comes from 1

Status
Not open for further replies.

imarosel

Technical User
Jun 23, 2005
149
US
If I have two datasets with the same variable names and I want to compare a variable in dataset one to a variable in dataset two is there a way to specify which dataset I'm looking at? The only solution I can think of is changing all the variable names from one set to something else.

Something like "If datset1.variable1 > dataset2.variable1
 
Yeah, changing the name of the variable is about the only way you can do this.
I may ave a shortcut for you. I recently wrote a macro to do exactly this for a vary large number of variables...
Code:
* Get list of columns in Dataset "TEST" where *;
* variable name starts with "QUE"             *;
* This method loads the list of column names  *;
* into a macro variable &COLS.                *;
proc sql noprint;
  select name
  into :cols separated by " "
  from dictionary.columns
  where memname = "TEST"
    and upcase(name) like "QUE%"
  ;
quit;
%put Columns to be updated: &COLS;

* Macro will loop though the columns listed in  *;
* macro variable &COLS renaming it by prefixing *;
* each variable with "TOT_"                     *;
%macro rnam();
  %do i = 1 %to &CNT;
     %let VAR = %SCAN(&COLS,&i);
     rename &VAR = TOT_&VAR;
  %end;
%mend rnam;

* Run proc Datasets and call the macro *;
proc datasets lib=work nolist;
  modify totals;
    %RNAM;
    rename population = tot_population;
run;
quit;
In this case, I was renaming each variable by appending TOT_ to the start of the name, ie QUE0001 became TOT_QUE0001.
There was also 1 extra variable "POPULATION" which needed to be renamed. I've left that in so you can see the syntax.
Let me know how it goes.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Thanks for the response, I'm using a similar method I found googling around. I still don't have this macro language down. Thanks again.

%macro rename(lib,dsn,suff);

proc sql noprint;
select nvar into :num_vars
from dictionary.tables
where libname="&LIB" and
memname="&DSN";
select distinct(name) into :var1-
:var%TRIM(%LEFT(&num_vars))
from dictionary.columns
where libname="&LIB" and
memname="&DSN";
quit;
run;
proc datasets library=&LIB;
modify &DSN;
rename
%do i=1 %to &num_vars;
&&var&i=&&var&i.._&suff
%end;
;
quit;
run;
%mend rename;
%rename(WORK,DRIVERTABLE,driver);,driver);
 
proc SQL anyone? Use the ALIAS option available in proc SQL to do exactly that. if you need some code examples I can supply some, but you should be able to find them out there.
Klaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top