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

Have some dynamic data sets with different variables each run.

Looking very similar to the below.

                             Banana_             Cherry             Ice                  Lemon          Heath_Bar
ID                           Split                  Sundae           Swirl                  Frost             Crunch
XCDF1483                1                        0                   1                         0                     0
JKTR48392               0                       1                    0                          0                    1
OPRT76231              1                       1                    0                          0                    1
VXIQ87126                1                       0                   1                          0                    0
KTUE61824               0                       1                   0                         1                     1

 

This code appears to be the solution  - for an array with variables that are all dates, each variable a different month.               

                                                                                                                          

data want;
set have;
_t=put(0,rb8.);
array t(*) months1-months6;/**group your months vars with a double dash*/
want = n(of t(*))-count(peekclong (addrlong(t[1]), 48),_t) ;
drop _t;
run;

 

My variables are not dates, and the name of the variables are quite different each time.

There would be maybe twenty variables at most.

                                                                                                                                                         

Result for each ID would be:   

XCDF1483             2

JKTR48392            2

OPRT76231           3

VXIQ87126            2

KTUE61824           3

 

How do I count - and store the total count of non-zero values/non-null values for each unique ID in the data set  as a new variable?   Not counting "0" or null. 

Again, result is stored in the data set as new variable "Total", or something similar.

 

Didn't use any string examples, but there will be some strings.

Note:  A variable is either formatted numeric (0 or 1, nothing else) or a char.

Character variables do not have numbers, and numerics do not have characters.

Longest string variable is 30 letters.

 

Thanx.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

From your example it looks like you just want to sum the numeric variables in your dataset.  So use the _NUMERIC_ variable list.

data want ;
  set have;
  total=sum(of _numeric_);
run;

If there are other numeric variables you want to exclude then you can remove those.  For example if had one ID variable and two date variables that you wanted to exclude the code might look like this:

data want ;
  set have;
  total=sum(of _numeric_,-sum(id,date1,date2));
run;

Note that SAS has two types of variables. Floating point numbers and fixed length character strings. The FORMAT attached to a variable just controls how the values are displayed but does not change the TYPE of the variable.

View solution in original post

3 REPLIES 3
sbxkoenk
SAS Super FREQ

Hello,

 

Is it this that you want?

data have;
LENGTH ID $ 30;
input ID $ Banana_Split Cherry_Sundae Ice_Swirl Lemon_Frost Heath_Bar_Crunch;
cards;
XCDF1483     1    0    1    0   0
JKTR48392    0    1    0    0   1
OPRT76231    1    1    0    0   1
VXIQ87126    1    0    1    0   0
KTUE61824    0    1    0    1   1
;
run;

data want;
 set have;
 array nume{*} _NUMERIC_;
 total=sum(of nume(*));
run;
/* end of program */ 

Koen

Tom
Super User Tom
Super User

From your example it looks like you just want to sum the numeric variables in your dataset.  So use the _NUMERIC_ variable list.

data want ;
  set have;
  total=sum(of _numeric_);
run;

If there are other numeric variables you want to exclude then you can remove those.  For example if had one ID variable and two date variables that you wanted to exclude the code might look like this:

data want ;
  set have;
  total=sum(of _numeric_,-sum(id,date1,date2));
run;

Note that SAS has two types of variables. Floating point numbers and fixed length character strings. The FORMAT attached to a variable just controls how the values are displayed but does not change the TYPE of the variable.

Jumboshrimps
Obsidian | Level 7
Did need the exclusion for date variables as there is a date value for every occurrence where the value = 1.
Awesome!!!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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