In regular 4gl I do:
data table_input;
X = 7; output;
X = 3; output;
X = 7; output;
X = 2; output;
X = 7; output;
X = 4; output;
X = 7; output;
X = 4; output;
X = 3; output;
X = 7; output;
run;
data _null_;
set table_input end = eof;
if _n_ = 1 then do;
length COUNT 8;
declare hash h (suminc:1, keysum:'COUNT');
h.definekey ('X');
h.definedone ();
end;
h.ref ();
if eof then h.output (dataset: 'table_output');
run;
I get the number of occurrences of the variable X as the results. It's ok.
How to do it in DS2? There is no keysum variable in DS2, I don't know how to add a Count that has the number of occurrences of X in the output table.
This code does not run properly as shown here. The DATA step with the hash object produces this error message:
85 length COUNT 8;
86 declare hash h (suminc:1, keysum:'COUNT');
87 h.definekey ('X');
88 h.definedone ();
89 end;
90 h.ref ();
91 if eof then h.output (dataset: 'table_output');
92 run;
NOTE: Variable COUNT is uninitialized.
ERROR: Type mismatch for method parameter 1 at line 86 column 25.
ERROR: Expecting Character type.
ERROR: DATA STEP Component Object failure. Aborted during the EXECUTION phase.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 2 observations read from the data set WORK.TABLE_INPUT.
Suminc: requires the name of a variable as input. Can you explain exactly what are you trying to accomplish with this code? What output do you expect?
Maybe it depends on the server settings, but on my machine this code works :).
I have SAS 9.4.6.
NOTE: Variable COUNT is uninitialized.
NOTE: The data set WORK.TABLE_OUTPUT has 4 observations and 2 variables.
NOTE: There were 10 observations read from the data set WORK.TABLE_INPUT.
Table table_output:
X COUNT
2 1
3 2
4 2
7 5
In the output I have the number of observations X in the table_input set.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.