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 ) :
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;
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.
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.