BookmarkSubscribeRSS Feed
kevsma
Quartz | Level 8

Hello, is there a way for me to fill in the missing category of my class variable with 0 in PROC SQL? I know PROC REPORT can do that, but it seems i can't find ways to work around it in PROC SQL. 

 

My code looks like below whereas eth includes 7 different groups, but for this one, only 6 of them have nonmissing results, therefore i am only seeing 6 groups after SAS ran PROC SQL. But I want to still have the 7th group but plug in 0s across all measures. 

 

is that doable? Thanks!!

 

PROC SQL;
	CREATE TABLE TBL26 AS
	SELECT eth, 
				COUNT(DISTINCT CASE WHEN(AMT=0) THEN UCI END) AS nopos,
				COUNT(DISTINCT CASE WHEN(AMT>0) THEN UCI END) AS yespos,
				COUNT(DISTINCT UCI) AS TOTCNT,
				CALCULATED NOPOS/CALCULATED TOTCNT AS SHARE
			FROM &Regctr.(WHERE=(AGE='00-02')) 
			GROUP BY eth
		UNION ALL
		SELECT 'Z',
			COUNT(DISTINCT UCI) AS TOTCNT,
			COUNT(DISTINCT CASE WHEN(AMT=0) THEN UCI END) AS NOPOS,
			COUNT(DISTINCT CASE WHEN(AMT>0) THEN UCI END) AS YESPOS,
			CALCULATED NOPOS/CALCULATED TOTCNT AS SHARE
		FROM &Regctr.(WHERE=(AGE='00-02'))
		

; QUIT;
1 REPLY 1
FreelanceReinh
Jade | Level 19

Hello @kevsma,

 

You can create a small auxiliary dataset which just contains all values of ETH. If all these values occur in the unrestricted dataset &Regctr., the preliminary step could look like this:

proc sql;
create table alleth as
select distinct eth from &Regctr.;
quit;

Otherwise INSERT additional ETH values into this dataset.

 

Then use the complete ALLETH dataset, e.g., in a RIGHT JOIN:

proc sql;
create table tbl26 as
select a.eth, 
       count(distinct case when(amt=0) then uci end) as nopos,
       count(distinct case when(amt>0) then uci end) as yespos,
       count(distinct uci) as totcnt,
       coalesce(calculated nopos/calculated totcnt,0) as share
   from &regctr.(where=(age='00-02')) natural right join alleth a
   group by a.eth
union all
select 'z',
       count(distinct case when(amt=0) then uci end) as nopos,
       count(distinct case when(amt>0) then uci end) as yespos,
       count(distinct uci) as totcnt,
       calculated nopos/calculated totcnt as share
   from &regctr.(where=(age='00-02'));
quit;

The COALESCE function can be omitted if you accept SHARE=. for the 0/0 case.

 

Note that I have corrected a mistake in your second SELECT statement: Without the CORR keyword of the UNION operator the variables would be matched by ordinal position, so with your code TOTCNT from the second SELECT would be matched with NOPOS from the first SELECT, NOPOS with YESPOS and so on! Alternatively (more robust), use the CORR keyword:

union all corr
select 'z' as eth,

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
  • 1 reply
  • 568 views
  • 0 likes
  • 2 in conversation