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

is there a function similar to Count in SPSS other than the SAS macro %COUNT created by Jim Groeneveld, OCS Biometric Support, the Netherlands in 2008?

 

here is a video of what Count does in SPSS

 

https://www.google.com/search?q=SPSS+COUNT&rlz=1C1CHBD_enUS919US919&oq=SPSS+COUNT&aqs=chrome..69i57....

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @Zad,

 

You could use the COUNT function or in some cases the COUNTC function.

 

Example:

data have;
do PSTATE_ADVEN_r=0.4, 4;
  do FUN=-4, 4;
    do POWER=4, 44;
      output;
    end;
  end;
end;
run;

data want;
set have;
n4=count(catx('||','#',PSTATE_ADVEN_r,FUN,POWER,'#'),'|4|');
run;

The COUNTC function would not work in the above example (but in others where, e.g., all values have length 1) because of the occurrence of digit 4 in values other than 4.

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Some of us can't actually view external videos from our work computer (firewall prevents us from doing so). Why don't you just tell us what this function does?

--
Paige Miller
Zad
Calcite | Level 5 Zad
Calcite | Level 5

I don't really know.  SPSS has this syntax 
COUNT Count4J=PSTATE_ADVEN_r FUN POWER(4).

 

From what I can tell it does this, for every entry of a 4 in the variable list count.  I will have to confirm by running in SPSS and the code below in SAS to compare the results.
if PSTATE_ADVEN_r=4 and FUN^=4 and POWER^=4 then count4J=1;
else if PSTATE_ADVEN_r^=4 and FUN=4 and POWER^=4 then count4J=1;
else if PSTATE_ADVEN_r^=4 and FUN^=4 and POWER=4 then count4J=1;
else if PSTATE_ADVEN_r=4 and FUN=4 and POWER^=4 then count4J=2;
else if PSTATE_ADVEN_r^=4 and FUN=4 and POWER=4 then count4J=2;
else if PSTATE_ADVEN_r=4 and FUN^=4 and POWER=4 then count4J=2;
else if PSTATE_ADVEN_r=4 and FUN=4 and POWER=4 then count4J=3;

PaigeMiller
Diamond | Level 26

@Zad wrote:

I don't really know.  SPSS has this syntax 
COUNT Count4J=PSTATE_ADVEN_r FUN POWER(4).

 

From what I can tell it does this, for every entry of a 4 in the variable list count.  I will have to confirm by running in SPSS and the code below in SAS to compare the results.
if PSTATE_ADVEN_r=4 and FUN^=4 and POWER^=4 then count4J=1;
else if PSTATE_ADVEN_r^=4 and FUN=4 and POWER^=4 then count4J=1;
else if PSTATE_ADVEN_r^=4 and FUN^=4 and POWER=4 then count4J=1;
else if PSTATE_ADVEN_r=4 and FUN=4 and POWER^=4 then count4J=2;
else if PSTATE_ADVEN_r^=4 and FUN=4 and POWER=4 then count4J=2;
else if PSTATE_ADVEN_r=4 and FUN^=4 and POWER=4 then count4J=2;
else if PSTATE_ADVEN_r=4 and FUN=4 and POWER=4 then count4J=3;


I see that @FreelanceReinh has given you a correct answer. However, for the future, I point out that an answer of "I don't really know" doesn't really advance our understanding and doesn't move us closer to providing help. SPSS syntax doesn't advance our understanding either. Either show us documentation for this COUNT function, or figure it out enough so you can explain.

--
Paige Miller
ballardw
Super User

Crappy video link. It is better to point to software document links.

 

Proc Freq will count the number of observations with each value in the data set.

If you have a SAS install then you should be able to run this code as the SASHELP.CLASS data set is provided by default for training.

Proc freq data= sashelp.class;
   tables sex age;  /*<- this "counts" the values of the variables in one dimension*/
  tables sex*age; /* counts in two dimensions, the intersections of the sex and age values*/
  tables sex*age / list;  /* displays two dimensions as a single row*/

/* and a different procedure*/
Proc tabulate data=sashelp.class;
   class sex age;
   tables sex,
             age*n;
  tables sex*age,
            n;
run;

Procedures Report and Means/Summary can also provide counts. Many analysis procedures will provide a data summary that may show value counts as well.

 

 

Note, that SPSS and SAS both have multiple ways to "count" just about anything. The details of what is needed will point to which procedure and/or options may be needed.

FreelanceReinh
Jade | Level 19

Hello @Zad,

 

You could use the COUNT function or in some cases the COUNTC function.

 

Example:

data have;
do PSTATE_ADVEN_r=0.4, 4;
  do FUN=-4, 4;
    do POWER=4, 44;
      output;
    end;
  end;
end;
run;

data want;
set have;
n4=count(catx('||','#',PSTATE_ADVEN_r,FUN,POWER,'#'),'|4|');
run;

The COUNTC function would not work in the above example (but in others where, e.g., all values have length 1) because of the occurrence of digit 4 in values other than 4.

Zad
Calcite | Level 5 Zad
Calcite | Level 5
I thought of using CAT but did not like that for my purposes.

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
  • 6 replies
  • 689 views
  • 0 likes
  • 4 in conversation