Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- Count Function in SPSS

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

☑ This topic is **solved**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 09-12-2023 08:32 AM
(224 views)

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

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

6 REPLIES 6

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@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

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

I thought of using CAT but did not like that for my purposes.

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

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.