BookmarkSubscribeRSS Feed
archibald
Obsidian | Level 7

Hi all, 

I would like to get a total of all roads (digit_road1 to digit_road4); When I used the sum  statement such as  total_digit=sum(of digit_road1-digit_road4)  I got the number of states 8 instead of 28 , which represents the number of occurence of roads.

Any help would be greatly appreciated. Thanks

See code below

data traffic;

  infile datalines;

  input state$ digit_road1 digit_road1 digit_road2 digit_road3 digit_road4 ;

datalines;

 

AL   101 103    .   345 

AK   205 590 500 700

AZ     . 100 390      .

IN   890 623 .       678

MD 755 400 222 678

IL   100 225 678 333

CA 800 455 900 567

TX 120 201 862 108;

 

3 REPLIES 3
Reeza
Super User

Where is 8 and 28 coming from based on your sample data?

 

Depending on your version of SAS you may need to reference the variable list as var1--var2 or var: to use all variables that start with the prefix "var".

 

I didn't think that would happen with the OF keyword though. In older versions of SAS you could be taking the sum of road1-road4 which is really just the substraction of the two variables.

 

What would you like your output to look like?

 

 

 

archibald
Obsidian | Level 7
8 is the number of cases, that is the number of observations (states).
I wat the output to be the total of all the digit_road variables.
PGStats
Opal | Level 21

Use the N function (count non missing arguments) and sum statements (retained and initialized at 0)

 

data traffic;
  infile datalines;
  input state$ digit_road1 digit_road2 digit_road3 digit_road4 ;
datalines;
AL   101 103    .   345 
AK   205 590 500 700
AZ     . 100 390      .
IN   890 623 .       678
MD 755 400 222 678
IL   100 225 678 333
CA 800 455 900 567
TX 120 201 862 108
;

data roads;
set traffic end=done;
roads + n(of digit_road1-digit_road4);
states + 1;
if done then output;
keep roads states;
run;

proc print data=roads noobs; run;
PG

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
  • 3 replies
  • 752 views
  • 1 like
  • 3 in conversation