BookmarkSubscribeRSS Feed
jenim514
Pyrite | Level 9

Hi!  I am interested in ouputing a sum value to a new variable based on a condition.

 

1. I have a table with 100 observations-  patientid with all their encounters.

2.  I want to sum all married patients with ER visit in each year 2012- 2014 and output that sum by year to the last.patientid of each year.

3. The end table should still have 100 observations...just with two new sum variables.

 

I've tried, but my table keeps reducing to one patientid with ER sum or the table stays at 100 with the ER sum on each observation line.

 

 

This is the last code i tried, but not correct.  Not  sure where to insert output statement (not correct here!) or if this type of do statement is possible??

 

DATA ER1_sum;
set var;
by patientid;
where  Marital_status='Married';
if ER1=1 then do;
    if year='2012' then do;
        if first.patientid=1 then n_ER12=0;
        n_ER12 +1;
        if last.patientid=1 then output;
 end;
    if year='2013' then do;
        if first.patientid=1 then n_ER13=0;
        n_ER13 +1;
        if last.patientid=1 then output;

end;

    if year='2014' then do;
        if first.patientid=1 then n_ER14=0;
        n_ER14 +1;
        if last.patientid=1 then output;

    end;

end;
run;

2 REPLIES 2
ballardw
Super User

Provide some example input and what you want for output for that input.

And are your sure that you need a dataset? This sounds like it should just be a report.

 

I also think that you mean to count your patients as it is very difficult to "sum all married patients".

jenim514
Pyrite | Level 9

Yes, count is correct.  I think I need a data set because i will need to build graphs later.  I think I can take out the marital status and just count the ER visits by year.  I can add a conditional statement later..

 

data for ER l is binary...if there was an ER visit the ER=1. 

 

ID 3. ER $ 1. Year $ 4. 

 

000  0  2012  

000  1  2014   

001  1  2012 

001  1  2012  

001  0  2013 

001  1  2013 

002  1  2012 

002  1  2014  

002  1  2014  

 

 

Want to look like this...

ID 3. ER $ 1. Year $ 4.  Marital $ 1. ER_12 1.  ER_13 1.  ER_14  1.  

000  0  2012 1 . .

000  1  2014  . . 1

001  1  2012  . . .

001  1  2012  . . .

001  1  2012 3 . .

001  0  2013  . . .

001  1  2013 .1 .

002  1  2012 1. .

002  1  2014 . . .

002  1  2014 . . 2

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!

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
  • 712 views
  • 0 likes
  • 2 in conversation