Hello. I have data in the wide format. There are 5,530 variables for each observation that contain 0, 1, 2, 3, 4, or missing. Those numbers represent a type of location for healthcare services (e.g., 1 = in patient, 2 = emergency department). I am trying to use the COUNT function to get the frequency for each of those values (0-4) for each observation. So far, I am not able to get the correct count.
To note, I did the simpler version of this and DID NOT specify which variables to count within, and I did get the correct number of those values, but that data step included those numbers from the PATID variable, which has 15-20 individual numbers.
Below is an example of the code for the data. Tos_Cd1 is the root variable name. There are 5,530 of these variables, hence the Tos_Cd11-Tos_Cd15530. Could someone please inform me where I can correct the code? Thank you!
data example1;
set example;
array string{*} Tos_Cd11-Tos_Cd15530;
do i=1 to dim(string);
HCRU_Other=countc(string(i),'0');
HCRU_IP=countc(string(i),'1');
*and so on...;
end;
run;
When something does not run, go to the log, copy the log with the submitted code and any error or warning messages and paste into a code box opened using the forum's {I} icon. The code box is important to maintain formatting of error messages as the message window will reformat text. Many errors will have an _ character on the line below the code to indicate the position SAS determined the error occurred. The message windows will move that character reducing the usefulness of the log.
Since I did not have a data set with 20,000 variables laying around I miscopied the modified smaller example I tested:
Instead of
HCRU_Other=countc(cats(of string(*),'0');
try
HCRU_Other=countc(cats(of string(*)),'0');
The CATS function wasn't closed.
See if this helps:
data example1; set example; array string{*} Tos_Cd11-Tos_Cd15530; HCRU_Other=countc(cats(of string(*),'0'); HCRU_IP=countc(cats(of string(*),'1');; run;
This assumes that the 0, 1 or whatever only occurs once per variable.
The above code creates one string value from all of the variables and then counts the characters in the resulting combined string.
Thanks for the response. Unfortunately, it did not run.
I added another ")" (below), and was getting the following error: ERROR 71-185: The COUNTC function call does not have enough arguments. Any ideas why?
data example1; set example; array string{*} Tos_Cd11-Tos_Cd15530;
HCRU_Other=countc(cats(of string(*),'0'));
HCRU_IP=countc(cats(of string(*),'1')); run;
When something does not run, go to the log, copy the log with the submitted code and any error or warning messages and paste into a code box opened using the forum's {I} icon. The code box is important to maintain formatting of error messages as the message window will reformat text. Many errors will have an _ character on the line below the code to indicate the position SAS determined the error occurred. The message windows will move that character reducing the usefulness of the log.
Since I did not have a data set with 20,000 variables laying around I miscopied the modified smaller example I tested:
Instead of
HCRU_Other=countc(cats(of string(*),'0');
try
HCRU_Other=countc(cats(of string(*)),'0');
The CATS function wasn't closed.
That did it! Thank you very much for the follow up and the help!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.