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

Hi y'all i hope you are all safe and coping the lockdown,

I know this topic had been discussed way times than it should be, but i really tried and fetched over the solutions for a few times before coming to you.

I'm working on a simple covid-19 data set containing these variables : 

Date  - Cases - Deaths - Country - Population - TotalCases

My problem is that i want to sum the number of cases by Country but i obtain very weird results.

First I sorted my data by Country and Date (by default order (ascending))

proc sort data=covid.data2;
by Country Date;
run;

Then I told sas to consider to start from the first case in cases and assign it to totalcases the sum until the last one is reached for each country :

data covid.data2;
set covid.data2;
by Country;
if First.cases then TotalCases = first.cases;
TotalCases + cases;
run;

However the results are absolutely not the sum of each case daily by country ! I can't seem to find the error, can you help ?

here is a screen of the output (an example of afghanistan ) :

Capture.PNG

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

first.cases does not exist; first.country is an automatic boolean variable that can only have the values 0 (false) or 1 (true).

You want

data covid.data3;
/* do not use the input dataset name here, unless you like destroyed datasets
that need to be re-created if something happens in the data step */
set covid.data2;
by country;
if first.country
then TotalCases = cases;
else TotalCases + cases;
run;

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
data covid.data2;
set covid.data2;
by Country;
if first.country then TotalCases = 0;
TotalCases + cases; 
run;

You want first.country, not first.case. The first time a country appears in the data set, the cumulative count is set to zero.

--
Paige Miller
Kurt_Bremser
Super User

first.cases does not exist; first.country is an automatic boolean variable that can only have the values 0 (false) or 1 (true).

You want

data covid.data3;
/* do not use the input dataset name here, unless you like destroyed datasets
that need to be re-created if something happens in the data step */
set covid.data2;
by country;
if first.country
then TotalCases = cases;
else TotalCases + cases;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 442 views
  • 3 likes
  • 3 in conversation