Help using Base SAS procedures

adding categories to the variable frequency (completetypes)

Accepted Solution Solved
Reply
Super Contributor
Posts: 301
Accepted Solution

adding categories to the variable frequency (completetypes)

Hi  all, I am trying to run this code, and the idea  is adding the rest of frequencies that

are not appearing in my final output *.

I try with completypes but it does not work for this example.

Any help?

data new;

input freq count treat;

datalines;

1 2 1

3 4 1

4 2 1

6 3 1

20 1 1

1 2 2

4 3 2

8 4 2

21 2 2

;

run;

proc sort data=new out=news; by freq; run;

proc transpose data=news out=newt (drop=_name_);

  by freq;

  id treat;

  var count;

run;

   proc summary data=newt nway completetypes;

             class freq;

             output out=want (drop=_type_ _freq_) idgroup(out(_1 _2)=);

           run;

proc print data=want; run;

*output:

                                     Obs    freq    _1    _2

                                       1       1      2     2

                                       2       3      4     .

                                       3       4      2     3

                                       4       6      3     .

                                       5       8      .     4

                                       6      20      1     .

                                       7      21      .     2

Thanks,

V


Accepted Solutions
Solution
‎01-30-2013 09:00 AM
Respected Advisor
Posts: 3,777

Re: adding categories to the variable frequency (completetypes)

Completetypes can only work with existing levels.  If you want to add levels that do not exisit in the data you have to make them.  CLASSDATA is one way to do that.


data new;
   input freq count treat;
   datalines;
1 2 1
3 4 1
4 2 1
6 3 1
20 1 1
1 2 2
4 3 2
8 4 2
21 2 2
;;;;
   run;
proc summary;
  
output out=mm(drop=_Smiley Happy min(freq)=min max(freq)=max;
  
run;
data classdata;
   set mm;
   do freq = min to max;
      output;
     
end;
  
stop;
  
run;
proc summary data=new classdata=classdata nway;
  
by treat;
   class freq;
   freq count;
   output out=filled(drop=_type_ rename=(_freq_=count));
   run;

View solution in original post


All Replies
Super Contributor
Posts: 543

Re: adding categories to the variable frequency (completetypes)

Hi,

Could you add in your output data set what exactly are you looking for?

Would you like the '?' to be filled??

Smiley Happy

Obs    freq    _1    _2

                                       1       1      2     2

                                       2       3      4    ?

                                       3       4      2     3

                                       4       6      3    ?

                                       5       8      ?     4

                                       6      20      1    ?

                                       7      21     ?     2

Super Contributor
Posts: 301

Re: adding categories to the variable frequency (completetypes)

freq (2nd column) I want to add: 2, 5, 7, 9, 10 , 11, 12, 13, 14, 15, 16, 17, 18, 19 ...but not manually, I would like to add in general the freqs that have missing values for both treatments _1 and _2.

Thanks.

Super Contributor
Posts: 543

Re: adding categories to the variable frequency (completetypes)

How about this?;

%macro insert();

%do i = 1 %to 19;

    data tmp_&i.;

        set newt(obs = 1) ;

        by freq;

        drop _1 _2;

            if first.freq then do;

                freq = &i.;

            output;

        end;

    run;

%end;

%mend insert;

%insert();

data final;

    merge newt tmp_:;

    by freq;

run;

Let me know if this is what you wanted;

Super Contributor
Posts: 301

Re: adding categories to the variable frequency (completetypes)

Many thanks Anca tilea for your help.

Solution
‎01-30-2013 09:00 AM
Respected Advisor
Posts: 3,777

Re: adding categories to the variable frequency (completetypes)

Completetypes can only work with existing levels.  If you want to add levels that do not exisit in the data you have to make them.  CLASSDATA is one way to do that.


data new;
   input freq count treat;
   datalines;
1 2 1
3 4 1
4 2 1
6 3 1
20 1 1
1 2 2
4 3 2
8 4 2
21 2 2
;;;;
   run;
proc summary;
  
output out=mm(drop=_Smiley Happy min(freq)=min max(freq)=max;
  
run;
data classdata;
   set mm;
   do freq = min to max;
      output;
     
end;
  
stop;
  
run;
proc summary data=new classdata=classdata nway;
  
by treat;
   class freq;
   freq count;
   output out=filled(drop=_type_ rename=(_freq_=count));
   run;

Super Contributor
Posts: 301

Re: adding categories to the variable frequency (completetypes)

Thanks data_null_, it was like I wanted.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 163 views
  • 4 likes
  • 3 in conversation