BookmarkSubscribeRSS Feed
mcook
Quartz | Level 8

I have several tables of just one variable.  

 

Table 1:                   

 

MasterList                  

A                                    
B
C
D
E
F
G

 

Table2:

 

Var1

A

B

E

G

 

Table3:

 

Var2:

B

C

E

F

 

with the rest similar.  With each table having some or all of the values in MasterList.  


I want to create a table with the variables (var1, var2, etc) as the observations, and the columns A, B,....,G from MasterList.

where the observation has a value of 1 if it contains that element from MasterList, and 0 if it does not.  

 

Like so.

             A     B   C   D   E   F   G

Var1     1      1    0   0   1    0   1

Var2     0      1    1   0   1    1   0

Var3     ...

 

any help appreciated.  

7 REPLIES 7
yabwon
Onyx | Level 15

Hi,

 

maybe like this (a view, format, tabulate, and a text file 😉

data Table1;
input MasterList $;
cards;
A
B
C
D
E
F
G
;
run;

data Table2;
input Var1 $;
cards;
A
B
E
G
;
run;
 

data Table3;
input Var2 $;
cards;
B
C
E
F
;
run;

data TMP/view = TMP;
  merge table:;
  array _X_(i) _character_;

  do over _X_;
    _name_ = vname(_X_);
    _value_ = _X_;
    output;
  end;
  keep _name_ _value_;
run;

proc format;
  value bool
  0,. = 0
  other = 1
  ;
run;

data _null_;
  set table1 end = eof;
  length list $ 1000;
  retain list;
  list = catx(" ", list, MasterList);
  if eof;
  call symputx("list", list, "G");
run;

ods _all_ close;
options ps = max ls = max nocenter nodate nonumber formchar="";
title;
filename f TEMP;
proc printto print = f;
run;
proc tabulate data = TMP;
  class _name_ _value_;
  table _name_=" ", _value_=" "*n=" "*f=bool.;
run;
proc printto;
run;
ods html;

data want;
  infile f firstobs = 6;
  input @@;
  put _infile_;
  if _infile_ ne "" then
    do;
      input var_name : $ 32. &list.;
      output;
    end;
  else input;
run;
proc print;
run;

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



mcook
Quartz | Level 8

Wow.  unfortunately that seems much more complicated and beyond my current understanding of SAS.  Might there be a different way?  

Shmuel
Garnet | Level 18

You can use next code:

data MasterList;
  infile cards;
  input key $;
cards;
A                                    
B
C
D
E
F
G
; run;

data table2;
  infile cards;
  input key $;
cards;
A                                    
B
E
G
; run;

data table3;
  infile cards;
  input key $;
cards;
B                                    
C
E
F
; run;

%macro assign;
   select (key);
    when ('A')  i=1;
	when ('B')  i=2;
    when ('C')  i=3;
	when ('D')  i=4;
    when ('E')  i=5;
	when ('F')  i=6;
	when ('G')  i=7;
	otherwise put '***Error ' key=;
   end;
   x(i) = 1;
%mend assign;

proc sql noprint;
  select key into :list1 separated by ' '
  from MasterList
  ;
  select key into :list2 separated by ','
  from MasterList
  ;
quit;

data want;
     retain &list1;
     drop i key;
     
     array x {*} &list1;
	 do i=1 to dim(x); x(i)=0; end;

  /* any non last table */	 
	 do until (eof);
	    set table2 end=eof;
		   %assign;
		   if eof then output;
	 end;
	 do i=1 to dim(x); x(i)=0; end; eof=0;

  /* last table */	 
	 do until (eof);
	    set table3 end=eof;
		   %assign;
		   if eof then output;
	 end;
	 
run;
	 
yabwon
Onyx | Level 15

Hi,

 

try this:

data TMP/view = TMP;
  merge table:;
  array _X_(i) _character_;

  do over _X_;
    _nm_ = vname(_X_);
    _value_ = _X_;
    output;
  end;
  keep _nm_ _value_;
run;

proc tabulate data = TMP out = o(keep = _nm_ _value_ N);
  class _nm_ _value_;
  table _nm_=" ", _value_=" "*n=" ";
run;

proc transpose data = o out = want(drop = _name_);
  by _nm_;
  var N;
  id _value_;
run;

data want;
  set want;
  array N _numeric_;
  do over N;
    N = (N > .);
  end;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Shmuel
Garnet | Level 18

Alternatively and simplified code will be:

proc sql noprint;
  select key into :list1 separated by ' '
  from MasterList
  ;
quit;

data want;
     retain &list1;
     cx = compress("&list1");
     drop i key cx;
     
     array x {*} &list1;
	 do i=1 to dim(x); x(i)=0; end;

  /* any non last table */	 
	 do until (eof);
	    set table2 end=eof;
		   i = indexc(cx,key);
		   x(i) = 1;
		   if eof then output;
	 end;
	 do i=1 to dim(x); x(i)=0; end; eof=0;

  /* last table */	 
	 do until (eof);
	    set table3 end=eof;
		   i = indexc(cx,key);
		   x(i) = 1;
		   if eof then output;
	 end;
run;

r_behata
Barite | Level 11
data Table1;
input MasterList $;
cards;
A
B
C
D
E
F
G
;
run;

data Table2;
input Var1 $;
cards;
A
B
E
G
;
run;

data Table3;
input Var2 $;
cards;
B
C
E
F
;
run;

data _null_;
	length vlst $25.;
	set Table1 end=done;
	retain vlst ' ';
	vlst=catx(' ',vlst,MasterList);
	put _all_;

	if done then
		call symputx('vlst',vlst);
run;

%put &vlst.;

data Test;
	array mlist[*] &vlst.;
	set Table2 Table3(rename=(var2=var1)) indsname=dsname;

	do i=1 to dim(mlist);
		if vname(mlist[i])=var1 then
			mlist[i]=1;
		else mlist[i]=0;
	end;

	varname=transtrn(scan(dsname,2),'TABLE','VAR');
	Drop i Var1;
run;

proc means data=test nway missing noprint;
	class varname;
	var &vlst.;
	output out= want(drop=_:) sum=;
run;
Ksharp
Super User
data Table1;
input MasterList $;
cards;
A
B
C
D
E
F
G
;
run;

data Table2;
input Var1 $;
cards;
A
B
E
G
;
run;

data Table3;
input Var2 $;
cards;
B
C
E
F
;
run;

%macro tran(dsn=,var=);
data temp;
 set &dsn;
 _&var=1;
run;
proc transpose data=temp  out=_&dsn;
id &var;
var _&var;
run;
%mend;

%tran(dsn=table1,var=MasterList)
%tran(dsn=table2,var=var1)
%tran(dsn=table3,var=var2)

data all;
 set _: ;
run;
proc stdize data=all out=want reponly missing=0;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1457 views
  • 1 like
  • 5 in conversation