没有过程的SAS中的频率

frequency in SAS without proc

我想在我的数据存储中创建一个频率,但我不知道如何创建。我不能使用程序。我想做这样的事情:

但现在我只有前两列。我不知道如何除以第二列中所有数据的总和。感谢您的帮助。

编辑: 我的代码获得前两列:

data dir.table1 (keep = gender summ);
set dir.table0;
by gender;
if first.gender then summ=0;
summ+1;
if last.gender then output;
run;

如 Reeza 所述,执行此操作的标准方法是 PROC FREQ。

至于其他解决方案:

DATASTEP

您不能使用一个 Datastep 执行此操作,因为您需要先计算总和。在下面的示例中,我使用 PROC SQL 计算总数并将其存储在宏变量中,稍后使用它来计算百分比。您还需要保留变量总和,这样它就不会在每次观察时都被重置。

/* SELECT total number of rows and store it in a macro variable */
PROC SQL ;
    SELECT COUNT(*) INTO :total FROM dir.table0;
QUIT;

DATA dir.table1 (KEEP=gender summ pct_tot);
    SET dir.table0;
    BY gender;

    /* Retain will keep the running total as you go through the dataset */
    RETAIN summ;

    IF first.gender THEN
        summ=0;

    summ+1;

    IF last.gender THEN DO;
        /* Calculate the percentage */
        pct_tot=summ / &total;
        OUTPUT;
    END;
RUN;

PROC SQL

恕我直言 SQL 通过对非协调子查询中的总记录数求和,提供了一种更简洁的获取频率的方法:

PROC SQL;
   CREATE TABLE dir.table1 AS 
   SELECT gender, 
          SUM(gender) AS summ, 
          SUM(gender) / (SELECT COUNT(*) FROM dir.table0) AS pct_tot
     FROM dir.table0
    GROUP BY gender;
QUIT;