BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
qqian
Calcite | Level 5

I have a table called 'topcode' which includes 2 codes in a column called 'code'. (will be more in reality, can reach 30)

I write a query to get the 2 code values (assume code62 and code400).

 

proc sql noprint;
select distinct code into : top2code separated by ' ' from topcode;
quit;

 

I have 2 other tables having the same structure as follows:

table 1

userscode1code5code62code44code400code999
user1111111
user2101010
user3000011
user4000110
user5101011

table 2

userscode2code5code30code62code400code500
user1110000
user2001010
user3001001
user4000100
user5001001

 

I want to loop over the topcode, and get the sum of 1s in each table and append them together.


%let i = 1;
%do i=1 %to %sysfunc(countw(&top2code));

proc sql;
select sum(%scan(&top2code, &i)) as flag1, count(*)-sum(%scan(&top2code, &i)) as flag0
from table1

UNION ALL
select sum(%scan(&top2code, &i)) as flag1, count(*)-sum(%scan(&top2code, &i)) as flag0
from table2
;
quit;

%end;

 

However, I can only get result for the first code, which code62.

flag1flag0
32
14

Is it possible to append code400 result below? such as:

flag1flag0
32
14
50
14

 

If not append, is there a way I can get result for all codes? Since there are more codes in reality, I have to use loop instead of calculate one by one manually.

 

Thanks,

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Why SQL and macros? Why not just use PROC MEANS and pass the variable list into the VAR statement? How do table1/table2 interact with each other? That's not clear.

Then your output will be in a single table anyways. Or you're combining just two which doesn't require a macro anyways.

View solution in original post

6 REPLIES 6
Reeza
Super User
Why SQL and macros? Why not just use PROC MEANS and pass the variable list into the VAR statement? How do table1/table2 interact with each other? That's not clear.

Then your output will be in a single table anyways. Or you're combining just two which doesn't require a macro anyways.
qqian
Calcite | Level 5
Thank you! This is helpful. And I can use the following code first to pull all the codes into top2code, then directly use it in the proc means var part.
proc sql noprint;
select distinct code into : top2code separated by ' ' from topcode;
quit;
The only problem is I can set N, SUM to get the total number and total 1s, and have to create N-SUM one more step to get the count of 0s.
But this is helpful! Thanks!!
Reeza
Super User
You could look at PROC FREQ then since you're only interested in counts really.
ChrisNZ
Tourmaline | Level 20

Like this?


%macro loop;

  %local i;

  proc sql;

  create table WANT as

  %do i=1 %to %sysfunc(countw(&top2code));

    %if &i>1 then UNION ALL;

    select sum(%scan(&top2code, &i)) as FLAG1, count(*)-sum(%scan(&top2code, &i)) as FLAG0
    from TABLE1

    UNION ALL

    select sum(%scan(&top2code, &i)) as FLAG1, count(*)-sum(%scan(&top2code, &i)) as FLAG0
    from TABLE2

  %end;

  ;
  quit;

%mend;

 

Regarding the missing rows, what's in the log?

 

ChrisNZ
Tourmaline | Level 20

Otherwise, as Reeza suggested, something like this would arguably be more legible, and faster:

proc sql noprint;
  select distinct cats('CODE',CODE) into :top2code separated by ' ' from TOPCODE;
quit;
data ALL/view=ALL;
  length SRC $4;
  set TABLE1(keep=&top2code in=A)
      TABLE2(keep=&top2code);
  src=ifc(A,'Tab1','Tab2');
run;
proc summary data=ALL nway;
  class SRC;
  var CODE: COUNT:;
  output out=SUM(keep=SRC CODE: _FREQ_ ) sum=;
run;

You can then, for example, use proc transpose and calculate the differences in a final data step.

 

 

qqian
Calcite | Level 5
Thank you Chris! It's also helpful.

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
  • 6 replies
  • 976 views
  • 0 likes
  • 3 in conversation