I need to assign seq number by group. I have tried using seq number but got it by one group (1,2,3, etc). However, I need it by two groups. As in the example below:
Have:
Var1 Var2 Var3
101 aaa 202
101 aaa 202
101 bbb 203
101 ccc 206
101 ddd 207
102 aaa 222
102 aaa 222
102 bbb 223
Want:
Obs var1 var2 var3 seq
1 101 aaa 202 1
2 101 aaa 202 1
3 101 bbb 203 2
4 101 ccc 206 3
5 101 ddd 207 4
6 102 aaa 222 1
7 102 aaa 222 1
8 102 bbb 223 2
If you sort your data it is quite simple:
proc sort data=sashelp.class out=class;
by sex age;
run;
data class;
set class;
by sex age;
if first.sex then
seqn = 0;
if first.age then
seqn + 1;
run;
Related
I'm trying to create a data set that will show me the duplicate transactions. The trouble I'm running into is when there are multiple orders on one order_id. The records that get assigned the 2s I would be considering the duplicate order.
data have;
input acct_id order_id;
datalines;
1 121
1 122
2 123
2 124
3 125
3 125
3 125
3 126
3 126
3 126
data want;
set have;
by acct_id order_id;
if first.acct_id then order_count = 1;
else order_count =2;
run;
My desired output is below.
acct_id | order_id | order_count
1 121 1
1 122 2
2 123 1
2 124 2
3 125 1
3 125 1
3 125 1
3 126 2
3 126 2
3 126 2
What I have coded out already I feel like is close but I can't get it figured out.
data want;
set have;
by acct_id order_id notsorted;
if first.acct_id then order_count=0;
if first.order_id then order_count+1;
put acct_id order_id order_count;
run;
acct_id order_id order_count
1 121 1
1 122 2
2 123 1
2 124 2
3 125 1
3 125 1
3 125 1
3 126 2
3 126 2
3 126 2
I have a dataset that looks like this
ID Model_Value Count_Model
111 24 2
222 12 9
234 88 6
111 88 8
222 24 10
222 88 17
I want it to look like this:
ID Model_12 Model_24 Model_88
111 0 2 8
222 9 10 17
234 0 0 6
I don't think I am searching online for the correct terms, I thought initially a transform might work but I still want the row to represent the ID not the model.
How do I go about creating this output from what I have?
Ok I believe this is it! Thank you #mjsqu !!
I was able to do this with the help of this link: http://www.sascommunity.org/mwiki/images/d/dd/PROC_Transpose_slides.pdf
data test_transpose ;
input #1 ID_P #6 Model_Value #18 Count_Model ;
cards;
111 24 2
222 12 9
234 88 6
111 88 8
222 24 10
222 88 17
run;
proc print data=test_transpose;
run;
proc sort data=test_transpose out=test_transpose_S;
By ID_P;
run;
proc transpose
data = test_transpose_S
out = test_transpose_result (drop=_name_)
prefix=Model_Value;
var Count_Model;
BY ID_P;
id Model_Value;
run;
proc print data=test_transpose_result ;
run;
Output of the original sorted dataset and the transpose!
data bob;
input subjid test$ lbdy flag1$;
datalines;
101 alt 1 e
101 alt 2 .
101 alt 3 e
101 alt 4 e
101 ast 1 e
101 ast 3 e
101 ast 4 .
101 ast 5 e
102 alt 1 e
102 alt 2 .
102 alt 3 e
102 alt 4 e
102 ast 1 e
102 ast 2 .
102 ast 3 e
102 ast 4 .
102 ast 5 e
102 ast 6 e
;
run;
I need to find the consecutive value
My expected output is
count
101 alt 1 e 1
101 alt 2 .
101 alt 3 e 2
101 alt 4 e 2
101 ast 1 e 2
101 ast 3 e 2
101 ast 4 .
101 ast 5 e 1
102 alt 1 e 4
102 alt 2 e 4
102 alt 3 e 4
102 alt 4 e 4
102 ast 1 e 1
102 ast 2 .
102 ast 3 e 1
102 ast 4 .
102 ast 5 e 2
102 ast 6 e 2
Thanks In Advance
I reckon this is what you want. Hard to know from cryptic description.
data bob;
input subjid test$ lbdy flag1$;
datalines;
101 alt 1 e
101 alt 2 .
101 alt 3 e
101 alt 4 e
101 ast 1 e
101 ast 3 e
101 ast 4 .
101 ast 5 e
102 alt 1 e
102 alt 2 e
102 alt 3 e
102 alt 4 e
102 ast 1 e
102 ast 2 .
102 ast 3 e
102 ast 4 .
102 ast 5 e
102 ast 6 e
;;;;
run;
proc sort data=bob;
by subjid test lbdy;
run;
proc print;
run;
data dob2;
do _n_=1 by 1 until(last.flag1);
set bob;
by subjid test flag1 notsorted;
end;
if not missing(flag1) then count = _n_;
do _n_ = 1 to _n_;
set bob;
output;
end;
run;
proc print;
run;
I have data that is set up like this:
Pers Year Month Variable Value
AAA 2001 01 Var1 100
AAA 2001 01 Var2 200
AAA 2001 06 Var1 110
AAA 2001 06 Var2 210
AAA 2002 01 Var1 120
AAA 2002 01 Var2 .
BBB 2001 01 Var1 100
BBB 2001 01 Var2 200
BBB 2001 06 Var1 110
BBB 2001 06 Var2 210
BBB 2002 01 Var2 220
I would like data that looks like this:
Pers Year Month Var1 Var2
AAA 2001 01 100 200
AAA 2001 06 110 210
AAA 2002 01 120 .
BBB 2001 01 100 200
BBB 2001 06 110 210
BBB 2002 01 . 220
How can I do this in SAS, preferably with proc transpose or sql?
Note that in the input data, above, Person BBB is missing an observation for 2002-01 Var1, but the output data has returned a missing value in the last line, i.e. ".".
Using proc transpose is the obvious solution.
proc transpose data=yourdata out=yourdatat1(drop=_name_);
by pers year month;
id variable;
var value;
run;
Using proc sql, you can use case when logic to summarize the data like below:
proc sql;
create table yourdatat2 as
select
pers,
year,
month,
sum(case when variable = 'Var1' then value else . end) as Var1,
sum(case when variable = 'Var2' then value else . end) as Var2
from
yourdata
group by
pers,
year,
month
;
quit;
In the following SAS statement, what do the parameters "noobs" and "label" stand for?
proc print data-sasuser.schedule noobs label;
per SAS 9.2 documentation on PROC PRINT:
"NOOBS - Suppress the column in the output that identifies each observation by number"
"LABEL - Use variables' labels as column headings"
noobs don't show you the column of observations number
(1,2,3,4,5,....)
my first title
results without noobs
Obs name sex group height weight
1 mike m a 21 150
2 henry m b 30 140
3 norian f b 18 130
4 nadine f b 32 135
5 dianne f a 23 135
results with noobs
my first title
name sex group height weight
mike m a 21 150
henry m b 30 140
norian f b 18 130
nadine f b 32 135
dianne f a 23 135