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

Hi Reader,

 

Can you please let me know how can I get attcahed output data.

Here is input data :

 

data abc;
input cd $ type $;
cards;
a new
a new
c new
a old
;

run;

 

requirement:

 

pdhokriya_0-1606572860186.png

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
proc sort data = want;
   by cd;
run;

data want (drop = n p);
   do until (last.cd);
      set want;
      by cd;
      if type = 'new' then do;
         new_n = n;
         new_p = p;
      end;
      else do;
         old_n = n;
         old_p = p;
       end;
   end;
   format new_p old_p percent8.2;
run;

Result:

 

cd new_n new_p   old_n  old_p 
a  2     66.67%  1      100.0% 
b  0     0.00%   0      0.00% 
c  1     33.33%  0      0.00% 

View solution in original post

14 REPLIES 14
PaigeMiller
Diamond | Level 26

Many of us will not download Microsoft Office files as they are security threats. Please show the desired output in your message.

--
Paige Miller
pdhokriya
Pyrite | Level 9
Posted in my question box
pdhokriya
Pyrite | Level 9

True, but considering future data this wont work.
As I wont to get values where records not present like "b", count would be for "a" and "c" records only.

PaigeMiller
Diamond | Level 26

@pdhokriya wrote:

True, but considering future data this wont work.
As I wont to get values where records not present like "b", count would be for "a" and "c" records only.


This is a case where additional requirements are mentioned after the initial question doesn't mention them. Can you please describe the entire requirements of the problem in one message?

 

In SAS, several procedures (TABULATE and REPORT and possibly others) allow a PRELOADFMT option which can produce a row of zeros if there are no values for a certain level of a variable.

--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

Are you only interested in the 'New' type or both new and old?

pdhokriya
Pyrite | Level 9
actually both, but its fine if you share for one.
PeterClemmensen
Tourmaline | Level 20

Ok. Just to be clear, do you want this in a SAS data set or in a report of some sort?

pdhokriya
Pyrite | Level 9
SAS dataset
PeterClemmensen
Tourmaline | Level 20

Ok. Here is one way. Let me know if it works for you

 

data abc;
input cd $ type $;
cards;
a new
a new
c new
a old
;

proc format;
   value $ fmt 'a' = 'a'
               'b' = 'b'
               'c' = 'c'
   ;
run;

proc summary data = abc nway completetypes;
   class type cd / preloadfmt;
   format cd $fmt.;
   output out = temp(drop = _TYPE_ rename = _FREQ_ = n);
run;

data want(drop = c);
   c = .;
   do until (last.type);
      set temp;
      by type;
      c + n;
   end;
   do until (last.type);
      set temp;
      by type;
      p = divide(n, c);
      output;
   end;
   format p percent8.2;
run;

 

Result:

 

type cd n  p 
new  a  2  66.67% 
new  b  0  0.00% 
new  c  1  33.33% 
old  a  1  100.0% 
old  b  0  0.00% 
old  c  0  0.00% 

 

PeterClemmensen
Tourmaline | Level 20

@pdhokriya did this solve your problem?

pdhokriya
Pyrite | Level 9
Yes, just a format issue i am getting it. could you please share how to i transpose these.

cd new_C new_p old_c old_p
a 2 66.67% 1 1 100.00%
b 0 0.00% 0 0 0.00%
c 1 33.33% 0 0 0.00%
PeterClemmensen
Tourmaline | Level 20
proc sort data = want;
   by cd;
run;

data want (drop = n p);
   do until (last.cd);
      set want;
      by cd;
      if type = 'new' then do;
         new_n = n;
         new_p = p;
      end;
      else do;
         old_n = n;
         old_p = p;
       end;
   end;
   format new_p old_p percent8.2;
run;

Result:

 

cd new_n new_p   old_n  old_p 
a  2     66.67%  1      100.0% 
b  0     0.00%   0      0.00% 
c  1     33.33%  0      0.00% 
pdhokriya
Pyrite | Level 9
@PeterClemmensen thank you so much for your quck reply. 🙂
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
  • 14 replies
  • 3255 views
  • 3 likes
  • 4 in conversation