Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- PROC SQL fill in missing class variable, similar to preloadfmt functio...

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 09-30-2023 02:29 AM
(335 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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 selecta.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 ®ctr.(where=(age='00-02'))natural right join alleth agroup bya.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 ®ctr.(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 allcorrselect 'z'as eth,

**Available on demand!**

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

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.