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

Hi All,

i have a data with level1,level2 level3,level4 categories in a variable. the data is keep on changing. i want to add missing whether the data as missing or not, but my final result reflect missing vategory.

I am using proc format, it is showing when data is missing, if data is no missing final out is not havinf missing category in my final dataset.

And i am ending with creating a dummy dataset to concatenate the with main data.

I am looking for alternative ways to create. Any thoughts will be helpful

example

data have;

input grp subgrp categ cnt;

cards;

3 1 level1 30

3 2 level2 50

3 3 level3 150

3 4 level4 56

3 5 level5 16

;

run;

want:

3 1 level1 30

3 2 level2 50

3 3 level3 150

3 4 level4 56

3 5 level5 16

3 6 missing 0(if data not having missing i need to show 0 or else i need to show how many are missing)

to get the category count i am using freq

proc freq data=rawdata;

  table stklevl/norow nocol missing out=have(rename=(stklevl=categ count=cnt));

format stklevl grpfmt.;

Thanks

Sam

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input grp subgrp categ $ cnt;
cards;
3 1 level1 30
3 2 level2 50
3 3 level3 150
3 4 level4 56
3 5 level5 16
;
run;
data want(drop=n);
 set have end=last;
 output;
 if missing(cnt) then n+1;
 if last then do;
  subgrp=_n_+1;
  categ='missing';
  cnt=n;
  output;
 end;
run;

Xia Keshan

View solution in original post

6 REPLIES 6
stat_sas
Ammonite | Level 13

Hi,

This is one way to add extra row

data have;
input grp subgrp categ $ cnt;
cards;
3 1 level1 30
3 2 level2 50
3 3 level3 150
3 4 level4 56
3 5 level5 16
;
run;

proc sql;
   insert into have
      set grp=3,
           subgrp=6,
          categ='missing',
          cnt=0;

quit;

proc print data=have;
run;

Reeza
Super User

Can you use proc tabulate with a preloadfmt instead?

http://support.sas.com/resources/papers/proceedings11/239-2011.pdf

Cynthia_sas
SAS Super FREQ

Preloadfmt works with PROC TABULATE, PROC REPORT and PROC MEANS.

cynthia

Ksharp
Super User
data have;
input grp subgrp categ $ cnt;
cards;
3 1 level1 30
3 2 level2 50
3 3 level3 150
3 4 level4 56
3 5 level5 16
;
run;
data want(drop=n);
 set have end=last;
 output;
 if missing(cnt) then n+1;
 if last then do;
  subgrp=_n_+1;
  categ='missing';
  cnt=n;
  output;
 end;
run;

Xia Keshan

sam369
Obsidian | Level 7

Hi Ksharp,

This is the exactly what i am looking for... Thank you !!!

Hi All,

Thank you for your valuable input..

Sam

data_null__
Jade | Level 19

This example show the basics of something from nothing with order.  To include categories that may not exist in the data use PRELOADFMT.   To establish row order and preserve it in PROC FREQ you need ORDER=DATA and MULTILABLE to convert the numeric categorical variable to character.  Otherwise PROC FREQ will move the numeric missing to the first row. There is no actual multi-labeling going on.  This example assumes you want the output from PROC FREQ but you can get this directly from out OUT= with TABULATE.

proc format;
  
value grpfmt(notsorted multilabel)
     
1='level1'
     
2='level2'
     
3='level3'
     
4='level4'
     
5='level5'
     
.='Missing'
      ;
   run;
data have;
   input grp subgrp categ $ cnt;
   cards;
3 1 level1 30
3 2 level2 50
3 3 level3 150
3 4 level4 56
3 5 level5 16
;;;;
run;
proc summary data=have nway missing completetypes;
  
class subgrp / preloadfmt mlf order=data;
   format subgrp grpfmt.;
  
freq cnt;
   output out=summary;
   run;     
proc print;
  
format _all_;
   run;
proc freq data=summary order=data;
   table subgrp  / norow nocol missing out=want noprint;
  
weight _freq_ / zeros;
  
run;
proc print;
  
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1127 views
  • 5 likes
  • 6 in conversation