English 中文(简体)
SAS 数据集
原标题:SAS Data Set Setting Variables of data set to add line numbers
  • 时间:2023-10-10 19:21:18
  •  标签:
  • sas
  • proc-sql

我有以下数据集:

Id com typ cust bu tar item item_sufx part line dtn_cd
10 ARF 3 2585 12 100 4587 800 1 1 1
10 ARF 3 2585 12 100 4587 800 1 2 1
10 ARF 3 2585 12 100 4587 800 1 3 2
22 XYZ 3 2585 12 100 4587 800 1 1 1
22 XYZ 3 2585 12 100 4587 800 1 2 2
22 XYZ 3 2585 12 100 4587 800 1 3 2
10 ARF 3 2585 12 100 4587 800 2 1 1
10 ARF 3 2585 12 100 4587 800 2 2 2
10 ARF 3 2585 12 100 4587 800 3 1 3
10 ARF 3 2585 12 100 4587 800 3 2 4
50 PFP 3 3000 12 100 9999 899 1 1 1
50 PFP 3 3000 12 100 9999 899 1 2 2
50 PFP 3 3000 12 100 9999 899 1 1 3
50 PFP 3 3000 12 100 9999 899 1 2 4

我希望以下产出:

“entergraph

我尝试了以下法典,但做得不好。

data table_A_mod;
set table_A;
by Id com typ cust bu tar item item_sufx part dtn_cd;
line_decreased = line_nbr < lag(line_nbr);
if first.part 
or line_decreased
then line_group+1;
if first.dtn_cd then drctn_seq=1; else drctn_seq+1;
run;

I get the following error

ERROR: BY variables are not properly sorted on data set WORK.Table_A.

I tried many combinations of "by" for the variables but nothing worked.

我在先前的这个问题上进一步阐述了这个问题,这里是 st流。 这就是为什么我的法典没有发挥作用。

任何想法如何做到这一点? 我对SAS或PROCQ(DB2)的建议持开放态度。

问题回答

如果你想对数据进行分类,就会进行分类。

proc sort data=have;
  by Id com typ cust bu tar item item_sufx part dtn_cd;
run;

data want ;
  set have;
  by Id com typ cust bu tar item item_sufx part dtn_cd;
  if first.id or line<lag(line) then group+1;
run;

Result

                                                        item_
Obs    id    com    typ    cust    bu    tar    item     sufx    part    line    dtn_cd    group

  1    10    ARF     3     2585    12    100    4587     800       1       1        1        1
  2    10    ARF     3     2585    12    100    4587     800       1       2        1        1
  3    10    ARF     3     2585    12    100    4587     800       1       3        2        1
  4    10    ARF     3     2585    12    100    4587     800       2       1        1        2
  5    10    ARF     3     2585    12    100    4587     800       2       2        2        2
  6    10    ARF     3     2585    12    100    4587     800       3       1        3        3
  7    10    ARF     3     2585    12    100    4587     800       3       2        4        3
  8    22    XYZ     3     2585    12    100    4587     800       1       1        1        4
  9    22    XYZ     3     2585    12    100    4587     800       1       2        2        4
 10    22    XYZ     3     2585    12    100    4587     800       1       3        2        4
 11    50    PFP     3     3000    12    100    9999     899       1       1        1        5
 12    50    PFP     3     3000    12    100    9999     899       1       2        2        5
 13    50    PFP     3     3000    12    100    9999     899       1       1        3        6
 14    50    PFP     3     3000    12    100    9999     899       1       2        4        6




相关问题
SAS stack overflow: PROC SQL reading dictionary.columns

I have a program in which I am reading dictionary.columns. There is a big program with lot of code before and after the program segment in which I read dictionary.column. The program used to work ...

SAS using encrypted (PWENCODE) in EMAILPW= option

My code works fine using plain text code, but fails when I use an encrypted password filename File email emailsys = VIM emailid= "&pa_usr" emailpw= "{sasenc}39AAD23E148A9555508AC84447181DFF" ; ...

How do I change the label in a data step header?

In SAS you can do. data a(rename=(a=b) ); a = 1; run; to rename a variable in the data step data statement (or data step header as I call it). What s the syntax to change the label? I tried ...

What s the easiest way to use SQLite with SAS?

I want to investigate how to access SQLite DB from SAS. What s the easiest way of doing this? Is there a SAS product that we can license to do that? I don t want to use ODBC drivers as that seems to ...

Computing Compounded Return in SAS

I have a dataset of date(monthly), person and return(monthly). I need to calculate the compounded monthly return of the dataset from April Year t to March Year t+1 for each person. For example, ...

Exchange Server and SAS 9.1.3/9.2

has anyone successfully interacted with a microsoft exchange server in SAS 9.1.3 or 9.2? i know it can be done with SAS Ent. Guide 4.x, but i m not interested in that route if regular SAS can do it.

Logging SAS scripts

I ve been developing a lot of Java, PHP and Python. All of which offer great logging packages (Log4J, Log or logging respectively). This is a great help when debugging applications. Especially if the ...

热门标签