BookmarkSubscribeRSS Feed
RandyStan
Fluorite | Level 6

Dear All

My Data is as follows

ID_BID_SVarA
AB10
AC10
CD10
CA10
DB15
AA5
CB10
BB20
AC10

The total of VarA is 100

 

I want to construct the following two tables
The first with the raw numbers

 ABCD
A510200
B02000
C1010010
D01500

 

And Then I want the Table in Percentages

 

 AB CD
      
A0.050.100.20
B00.2 00
C0.10.1000.1
D00.15 00

 

 

Thanks in advance

Randy

5 REPLIES 5
Reeza
Super User

Do you need data sets or displayed reports as your output?

PROC FREQ will do this, have you tried that so far, and if so, post what code you did try and let us know what didn't work. 

Are your percents, percent of total, row or column?

 

Can you provide your data set as data steps?

Here are instructions on how to provide sample data as a data step:
https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 


@RandyStan wrote:

Dear All

My Data is as follows

ID_B ID_S VarA
A B 10
A C 10
C D 10
C A 10
D B 15
A A 5
C B 10
B B 20
A C 10

The total of VarA is 100

 

I want to construct the following two tables
The first with the raw numbers

  A B C D
A 5 10 20 0
B 0 20 0 0
C 10 10 0 10
D 0 15 0 0

 

And Then I want the Table in Percentages

 

  A B   C D
           
A 0.05 0.1 0 0.2 0
B 0 0.2   0 0
C 0.1 0.1 0 0 0.1
D 0 0.15   0 0

 

 

Thanks in advance

Randy


 

yabwon
Amethyst | Level 16

As I wrote ons SAS-L, try this one:

 

ods html;
data have;
input
ID_B $        ID_S  $       VarA;
cards;
A               B              10
A               C              10
C               D              10
C               A              10
D               B              15
A               A                5
C               B              10
B               B              20
A               C              10
;
run;
proc print;
run;

proc format;
value blankaszero
. = 0
other = [best32.]
;

picture pctfmt
. = "0" (noedit)
low-high='9.99' (multipl=1)
;
run;


proc tabulate data = have ;
class id_:;
var varA;
table id_B=" ", id_s=" "*varA=" "*sum=" "*f=blankaszero. ;

table id_B=" ", id_s=" "*varA=" "*reppctsum=" "*f=pctfmt12.;
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



Ksharp
Super User

Do you want a table or just a report ?

 

data have;
input
ID_B $        ID_S  $       VarA;
cards;
A               B              10
A               C              10
C               D              10
C               A              10
D               B              15
A               A                5
C               B              10
B               B              20
A               C              10
;
run;
proc summary data=have nway;
class id_b id_s;
var vara;
output out=temp sum=;
run;

options missing='0';
proc transpose data=temp out=table1(drop=_:);
by id_b;
id id_s;
var vara;
run;
data table2;
 set table1;
 array x{*} _numeric_;
 do _n_=1 to dim(x);
   x{_n_}=x{_n_}*.01;
 end;
run;
Watts
SAS Employee
proc freq data=have;

     /* table displays only frequencies */
     tables ID_B * ID_S / nopct norow nocol;

     /* table displays only percents */
     tables ID_B * ID_S / nofreq norow nocol;

     weight VarA;

run;
hashman
Ammonite | Level 13

@RandyStan:

If you want reports, follow the advice of @yabwon or @Watts

 

If you want files, the solution by @Ksharp will work. Alternatively, if you want to save some I/O and solve in 2 passes through the input file (cannot be done in just one), consider:

data have ;                                                                                                                                                                                                                                                     
  input (ID_B ID_S) (:$1.) VarA ;                                                                                                                                                                                                                               
  cards ;                                                                                                                                                                                                                                                       
A  B  10                                                                                                                                                                                                                                                        
A  C  10                                                                                                                                                                                                                                                        
C  D  10                                                                                                                                                                                                                                                        
C  A  10                                                                                                                                                                                                                                                        
D  B  15                                                                                                                                                                                                                                                        
A  A   5                                                                                                                                                                                                                                                        
C  B  10                                                                                                                                                                                                                                                        
B  B  20                                                                                                                                                                                                                                                        
A  C  10                                                                                                                                                                                                                                                        
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
proc sql noprint ;                                                                                                                                                                                                                                              
  select unique id_s into :v separated by " " from have ;                                                                                                                                                                                                       
quit ;                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                
data _null_ ;                                                                                                                                                                                                                                                   
  array v &v ;                                                                                                                                                                                                                                                 
  if _n_ = 1 then do ;                                                                                                                                                                                                                                          
    dcl hash h () ;                                                                                                                                                                                                                                             
    h.definekey  ("id_b") ;                                                                                                                                                                                                                                     
    h.definedata ("id_b") ;                                                                                                                                                                                                                                     
    do over v ;                                                                                                                                                                                                                                                
      h.definedata (vname (v)) ;                                                                                                                                                                                                                               
    end ;                                                                                                                                                                                                                                                       
    h.definedone () ;                                                                                                                                                                                                                                           
  end ;                                                                                                                                                                                                                                                         
  set have end = lastobs ;                                                                                                                                                                                                                                      
  vsum ++ vara ;                                                                                                                                                                                                                                                
  if h.find() ne 0 then call missing (of v[*]) ;                                                                                                                                                                                                               
  do over v ;                                                                                                                                                                                                                                                  
    if vname (v) = id_s then v ++ vara ;                                                                                                                                                                                                                      
  end ;                                                                                                                                                                                                                                                         
  h.replace() ;                                                                                                                                                                                                                                                 
  if lastobs ;                                                                                                                                                                                                                                                  
  h.output (dataset:"table1") ;                                                                                                                                                                                                                                 
  dcl hiter hi ("h") ;                                                                                                                                                                                                                                          
  do while (hi.next() = 0) ;                                                                                                                                                                                                                                    
    do over v ;                                                                                                                                                                                                                                                
      v = divide (v, vsum) ;                                                                                                                                                                                                                                  
    end ;                                                                                                                                                                                                                                                       
    h.replace() ;                                                                                                                                                                                                                                               
  end ;                                                                                                                                                                                                                                                         
  h.output (dataset:"table2") ;                                                                                                                                                                                                                                 
run ;                                         

Kind regards

Paul D. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 1238 views
  • 1 like
  • 6 in conversation