data TABLE;
set WORK.TABLENAME;
by Sex;
if first.Sex then SEQUENCE = 1;
else SEQUENCE + 1;
run; Quit;
As many people in this thread have already said, you have chosen the wrong tool. SQL is not the right tool, and requesting that you want code to do it in SQL doesn't change the fact that SQL is not the right tool.
This topic has been discussed so many times in this forum.
If you really want it by SQL, try ODS skill.
data have; set SASHELP.CARS; keep make; run; ods select none; ods output SQL_Results=SQL_Results; proc sql number; select * from have order by make; quit; ods select all; proc sql; create table want as select *,row-min(row)+1 as seq from SQL_Results group by make order by row; quit;
@Prashan wrote:
Consider SASHELP.CARS dataset and give the sequence number for MAKE variable.
Ex:- in MAKE variable, I want sequence number for AUDI
... stuff deleted ...
that too only with PROC SQL, not with data step, I know how to do with data step.
I think there are three statements relevant to this problem.
And if this is some kind of homework, tell your teacher that the purpose of a SAS course is to learn the efficient use of the SAS system, not wasting time with stupid things you'll never do in real life.
Hi @thanikondharish , this is a solution using data step (not sql) base on the codes, suggestions, and comments from others in previous threads:
data hw;
input Name $ Sex $ Age Height Weight;
datalines;
Alice F 13 56.5 84
Barbara F 13 65.3 98
Carol F 14 62.8 102.5
Jane F 12 59.8 84.5
Janet F 15 62.5 112.5
Joyce F 11 51.3 50.5
Judy F 14 64.3 90
Louise F 12 56.3 77
Mary F 15 66.5 112
Alfred M 14 69 112.5
Henry M 14 63.5 102.5
James M 12 57.3 83
Jeffrey M 13 62.5 84
John M 12 59 99.5
Philip M 16 72 150
Robert M 12 64.8 128
Ronald M 15 67 133
Thomas M 11 57.5 85
William M 15 66.5 112
;
run;
proc print data=hw;run;
data hwseq;
set hw;
by sex;
seq+1;
if first.sex then seq=1;
run;
proc print data=hwseq;run;
But @dxiao2017 .
The dataset example you are using is already sorted by SEX, as presented by the OP. But that may be unlike most situations (and unlike sashelp.class which is the source of the data example, and is sorted by name, not by sex/name.).
Besides there is no need to sort the data by sex, merely to generate within-sex sequence numbers.
For instance:
data want (drop=_:);
set sashelp.class;
_nm+(sex='M');
_nf+(sex='F');
sequence=ifn(sex='M',_nm,_nf);
run;
And if one MUST use SQL, then the undocumented (read "unsupported") MONOTONIC() function (see MONOTONIC-function-in-PROC-SQL) can be used for each sex:
proc sql;
create table want as
select name, monotonic() as seq from sashelp.class where sex='M'
union corr
select name, monotonic() as seq from sashelp.class where sex='F'
;
quit;
But note that data order in the case of PROC SQL will almost certainly not be the same as in the original data set. And that the row order will actually change depending on the order of variables
Many of us advise against using undocumented features such as MONOTONIC because of the reasons stated by @mkeintz and also because when the next release of SAS comes out, the same code may provide different results, or it may not even work. In fact, even within the same release of SAS, it might work properly on data set A and not work correctly on data set B. So you use it at your own risk.
Furthermore, I'm sure that if any auditor of my code (I work at a bank where code can be audited by either internal or external auditors), using undocumented features of a computer language would cause my code to fail the audit.
Calling all data scientists and open-source enthusiasts! Want to solve real problems that impact your company or the world? Register to hack by August 31st!
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.