BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

 

I am trying to calculate number of subjects for dose and Total number in my data. I got the values i need in my output except for "Total " variable. In the program below, there are 6 distinct subjects in the data. But I am getting 8 for total value. Please help in my code to get 6 for 'Total' variable.

Thank you

data one;
input id term$ dose$;
datalines;
1 hache 10mg
1 sache 20mg
2 epain 20mg
2 lpain 10mg
3 rash placebo
4 coli 40mg
5 capin 40mg
6 naus 40mg
6 vomit 20mg
;

proc sql;
create table two as
select count(distinct ID) as NS,dose
from one
where dose in ('10mg' '20mg' '40mg' 'placebo')
group by dose;
quit; 

proc transpose data=two out=three(drop=_NAME_);
  var NS;
  id dose;
run;

data four;
  set three;
  array data  _10mg _20mg _40mg placebo;
  do over data;
    if missing(data) then data=0;
  end;
  TOTAL=_10mg + _20mg + _40mg ;
run;

 

2 REPLIES 2
mkeintz
PROC Star

You've made a good case for the UNION operator in SQL (assuming you must  use SQL).

 

proc sql;

  create table two as

   select count(distinct ID) as NS,dose

   from one

   where dose in ('10mg' '20mg' '40mg' 'placebo')

   group by dose

 union

   select count(distinct ID) as NS, 'TOTAL' as dose

   from one

  ;

quit;

 

proc transpose data=two out=three(drop=_NAME_);

var NS;

id dose;

run;

 

Also you don't need to follow the proc transpose with a data step.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Reeza
Super User

If you're using a report procedure to summarize, it can add the total.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 2 replies
  • 3595 views
  • 2 likes
  • 3 in conversation