Macro Associative Array

From sasCommunity
Jump to: navigation, search

This is an associative array implemented with macro allocation statements:

see also: Macro_assoc

* store these allocations in e.g.: autoexec.sas;
%Let B = Company B;
%Let C = Company C;
%Let D = Company D;
 
* within a program where you have foreign key site;
%Let Site = C;
%Let SiteName = &&&Site.; * yes, triple ampersands;
%Put SiteName:&SiteName.;
6    %Let SiteName = &&Site.;*only two ampersands;
7    %Put SiteName:&SiteName.;
SiteName:C
8    %Let SiteName = &&&Site.;* correct resolution: 3 ampersands;
9    %Put SiteName:&SiteName.;
SiteName:Company C

per M. Howard post to SAS-L on 2009-Dec-15

another way to do this, using data step and sql:

DATA Work.Values;
infile cards missover;
input site 1-1 sitename 3-30
cards;
A CompanyA
B CompanyB
C CompanyC
;
run;
 
*Then whenever you need it, use SQL to go get it:;
 
%Let Site = A;
 
PROC SQL noprint;
         select sitename into :sitename
         from Work.Values
         where site="%upcase(&site.)";
quit;
%put &sitename;

per Chang Chung post to SAS-L on 2009-Dec-15

proc format;
  value $sitename
    "A" = "Test 1"
    "B" = "Abc"
    "C" = "XYZ"
    "D" = "Tst Health"
    other = "Invalid Code";
run;
 
%macro sitename(sitecode);
  %sysfunc(putc(&sitecode,$sitename))
%mend  sitename;
 
/* check */
%let site = A;
%put site=&site name=%sitename(&site);
%let site = D;
%put site=&site name=%sitename(&site);
/* on log
site=A name=Test 1
site=D name=Tst Health
*/


--macro maven == the radical programmer 21:03, 15 December 2009 (UTC)