I have a table with list of account numbers bucket n monthperiod. I need to make a bucket string like below..please help (base SAS)
ACC Bucket Month bucketstring
123 0 jan18 0
123 1 feb18 10
123 2 mar18 210
345 0 feb18 0
345 1 mar18 10
The retain statement is used to maintain the value of a non-set variable over the iterations of the implicit loop that happens during the DATA step.
This example will work with ACC groups having upto 15 months (0..15). ACCs with more months will see a message put in the log.
data want;
set have;
by ACC;
length bucketstring $20; * bucketstring might have to be made longer;
retain buckstring;
if length (bucketstring) = 20 and not first.ACC then
put 'ERROR: bucketstring has to be longer for the case of ' ACC= month=;
if first.ACC
then bucketstring = cats(month);
else bucketstring = cats(bucketstring,month);
run;
The cats function concatenates items. The items are automatically stripped of leading and trailing spaces, as well as automatically converting a number-item to a character value if necessary.
Related
I'm working in SAS as a novice. I have two datasets:
Dataset1
Unique ID
ColumnA
1
15
1
39
2
20
3
10
Dataset2
Unique ID
ColumnB
1
40
2
55
2
10
For each UniqueID, I want to subtract all values of ColumnB by each value of ColumnA. And I would like to create a NewColumn that is 1 anytime 1>ColumnB-Column >30. For the first row of Dataset 1, where UniqueID= 1, I would want SAS to go through all the rows in Dataset 2 that also have a UniqueID = 1 and determine if there is any rows in Dataset 2 where the difference between ColumnB and ColumnA is greater than 1 or less than 30. For the first row of Dataset 1 the NewColumn should be assigned a value of 1 because 40 - 15 = 25. For the second row of Dataset 1 the NewColumn should be assigned a value of 0 because 40 - 39 = 1 (which is not greater than 1). For the third row of Dataset 1, I again want SAS to go through every row of ColumnB in Dataset 2 that has the same UniqueID as in Dataset1, so 55 - 20 = 35 (which is greater than 30) but NewColumn would still be assigned a value of 1 because (moving to row 3 of Datatset 2 which has UniqueID =2) 20 - 10 = 10 which satisfies the if statement.
So I want my output to be:
Unique ID
ColumnA
NewColumn
1
15
1
1
30
0
2
20
1
I have tried concatenating Dataset1 and Dataset2 into a FullDataset. Then I tried using a do loop statement but I can't figure out how to do the loop for each value of UniqueID. I tried using BY but that of course produces an error because that is only used for increments.
DATA FullDataset;
set Dataset1 Dataset2; /*Concatenate datasets*/
do i=ColumnB-ColumnA by UniqueID;
if 1<ColumnB-ColumnA<30 then NewColumn=1;
output;
end;
RUN;
I know I'm probably way off but any help would be appreciated. Thank you!
So, the way that answers your question most directly is the keyed set. This isn't necessarily how I'd do this, but it is fairly simple to understand (as opposed to a hash table, which is what I'd use, or a SQL join, probably what most people would use). This does exactly what you say: grabs a row of A, says for each matching row of B check a condition. It requires having an index on the datasets (well, at least on the B dataset).
data colA(index=(id));
input ID ColumnA;
datalines;
1 15
1 39
2 20
3 10
;;;;
data colB(index=(id));
input ID ColumnB;
datalines;
1 40
2 55
2 30
;;;;
run;
data want;
*base: the colA dataset - you want to iterate through that once per row;
set colA;
*now, loop while the check variable shows 0 (match found);
do while (_iorc_ = 0);
*bring in other dataset using ID as key;
set colB key=ID ;
* check to see if it matches your requirement, and also only check when _IORC_ is 0;
if _IORC_ eq 0 and 1 lt ColumnB-ColumnA lt 30 then result=1;
* This is just to show you what is going on, can remove;
put _all_;
end;
*reset things for next pass;
_ERROR_=0;
_IORC_=0;
run;
I have a dataset that contains an ID and some additional data. I want to perform transformations based on the ID with a by statement. The transformation works. Unfortunately SAS automatically reduces the dataset to one row per group. Does anybody know how to keep the original (number of) rows and still perform the group actions?
Here is some sample code to illustrate my problem
data dat;
input ID X $;
datalines;
1 a
1 b
1 c
1 d
2 a
2 b
3 a
4 k
5 z
5 a
5 c
;
data dat_new;
length x_new $2100.;
do until(last.ID);
set dat;
by ID notsorted;
x_new = ',' ||catx(',',x,x_new);
end;
drop x;
run;
Just add an OUTPUT statement inside the DO loop.
data dat_new;
length x_new $2100.;
do until(last.ID);
set dat;
by ID notsorted;
x_new = ',' ||catx(',',x,x_new);
output;
end;
drop x;
run;
When you do not have an explicit OUTPUT statement in a data step then an implied OUTPUT statement executes at the end of the data step. Your DO loop around the SET statement means that the end of the data step is only reached for the last observation per group.
If you want the final calculated value to be replicated on each observation then just add another loop to re-read the observations and put the OUTPUT statement in that loop.
data dat_new;
length x_new $2100.;
do until(last.ID);
set dat;
by ID notsorted;
x_new = ',' ||catx(',',x,x_new);
end;
do until(last.ID);
set dat;
by ID notsorted;
output;
end;
drop x;
run;
When you want to associate a group level computation result to EACH row in the group you will need to first iterate over the group to compute the result, and then have a second loop that reads the same rows of the group and outputs each. Use additional variables if you need to know the sequence number within the group and the total number of rows in the group.
data want(keep=id x_csv_list by_group_size seq);
length x_csv_list $2100.;
do by_group_size = 1 by 1 until(last.ID);
set dat;
by ID notsorted;
x_csv_list = catx(',',x_csv_list,x);
end;
do seq = 1 to by_group_size;
set dat;
output;
end;
run;
Also, if you are at the 'never really get it' stage, remember NOTSORTED means contiguous rows with the same by group variable values.
by s
s group first.s last.s
- ----- ------- ------
A 1st 1 0
A 1st 0 0 /* trick knowledge both 0 means row is interior */
A 1st 0 1
B 2nd 1 1 /* trick knowledge both 1 means group size is 1 row */
A 3rd 1 0
A 3rd 0 1
B 4th 1 0
B 4th 0 0
B 4th 0 1
C 5th 1 0
C 5th 0 1
I'm writing a program in SAS.
Here's the dataset I have:
id huuse days
1 0 4
1 0 3
1 1 12
1 1 1
1 2 15
2 1 13
2 0 16
2 1 18
2 0 44
For each ID, I want to delete the record if variable huuse ne 1, until I get to the first huuse=1. Then I want to keep that record and all subsequent records for that id, no matter what value huuse is. So for id=1, I want to delete the first two records than keep all records for id=1 starting with the 3rd record. For id=2, the first record has huuse=1, so I want to keep all records for id=2.
The data set I want should look like this:
id huuse days
1 0 4
1 0 3
1 1 12
1 1 1
1 2 15
2 1 13
2 0 16
2 1 18
2 0 44
I tried this code, but it removes all records that have huuse ne 1.
data want;
set have;
by id;
do until (huuse=1);
if huuse = 1 then LEAVE;
if huuse ne 1 then DELETE;
END;
run;
I've tried several variations of do loops, but they all do the same thing.
The DATA step is a program with an implicit loop that reads every record of the data set specified in the SET statement. Any program data vector (pdv) variables not coming from the data set are, by default, reset to missing at the top of the implicit loop. You change that behavior using a RETAIN statement to name variables that should not get reset.
So, in your problem you have two situations when a tracking variable is needed. The variable will track the state of the condition Have I seen huuse=1 yet in this group ?. Call this variable one_flag
RETAIN one_flag; so you control when it's value changes
At the start of a BY group one_flag needs to be reset to false (0)
When huuse is first seen as 1 set the flag to true (1)
Example:
data want(drop=one_flag);
set have;
by id;
retain one_flag 0;
if first.id then one_flag = 0;
if not one_flag and huuse = 1 then one_flag = 1;
if one_flag then OUTPUT; * want all rows in group starting at first huuse=1;
run;
You can place the SET and BY statement inside an explicit DO and that changes the operating behavior of the program, especially if the explicit loop is terminated according to a LAST.<var> automatic variable. Such a loop is commonly called a DOW loop by SAS programmers. There is no phrase DOW loop in the SAS documentation.
Example:
data want;
do until (last.id);
set have;
by id;
if not one_flag and huuse=1 then one_flag = 1;
if one_flag then OUTPUT; * want all rows in group starting at first huuse=1;
end;
run;
Because the looping is explicit and never reaches the TOP of the program with in the loop, there is no need to RETAIN the flag variable, nor reset it. Program variables that are not retained are reset automatically at the top of the program, and the top of the program is only reached at the start of the BY group. Learn more about this programming construct in the SGF 2013 paper "The Magnificent DO", Paul M. Dorfman
Your source and result are same :-)
But if I understood your question correctly the solution is quite simple with a retain solution. I add 2 lines to the example to make it clear that I understood correctly.
The code with example table:
data test;
id=1;huuse=0;days=4;output;
id=1;huuse=0;days=3;output;
id=1;huuse=1;days=12;output;
id=1;huuse=1;days=1;output;
id=1;huuse=2;days=15;output;
id=2;huuse=1;days=13;output;
id=2;huuse=0;days=16;output;
id=2;huuse=1;days=18;output;
id=2;huuse=0;days=44;output;
id=3;huuse=0;days=1;output;
id=3;huuse=1;days=2;output;
run;
data test_output;
set test;
retain keep_id -1;
if (keep_id ne id and huuse ne 0) then keep_id=id;
if keep_id = id then output;
run;
/* the results:
id huuse days
1 1 12 1
1 1 1 1
1 2 15 1
2 1 13 2
2 0 16 2
2 1 18 2
2 0 44 2
3 1 2 3
*/
I have a dataset similiar to this.I am not sure how many coulmns I would get or rows as it is part of the code. But I will have the first value to be equal to 0 bucket.
DATA MY_data;
INPUT bucket D_201503 D_201504 ;
DATALINES;
0 1000 20500
1 200 6700
2 101 456
3 45 567
;
eg -In this dataset I want the values below 10% of the first row value should be missing. like for eg first value is 1000 for bucket 0 so 45 should be missing. The same for 20500 as well.Anything below 10% should be missing. The dataset is generally not huge but need to determine columns and rows.
So I should get this as
0 1000 20500
1 200 6700
2 101 .
3 . .
I am not sure how I should loop through the dataset and make this condition
DATA MY_data;
INPUT bucket D_201503 D_201504 ;
DATALINES;
0 1000 20500
1 200 6700
2 101 456
3 45 567
;
data want;
set MY_data;
array row(*) _all_;
array _first_row(999); /*any number >= the number of columns of MY_data*/
/*we read the first line and store the values in _first_row array*/
retain _first_row:;
if _n_ = 1 then do i=1 to dim(row);
_first_row(i) = row(i);
end;
/*replacing values <10% of the first row*/
else do i=1 to dim(row);
if upcase(vname(row(i))) ne "BUCKET" and row(i) < 0.1*_first_row(i) then row(i) = .;
end;
drop i _first_row:;
run;
/*Find out how many variables there are (assume we just want all vars prefixed D_)*/
data _null_;
set my_data(obs = 1);
array vars_of_interest(*) D_:;
call symput(dim(vars_of_interest),"nvars")
run;
/*Save bucket 0 values to a temp array, compare each row and set missing values*/
data want;
set my_data;
array bucket_0(&nvars) _temporary_;
array vars_of_interest(*) D_:;
do i = 1 to &nvars;
if bucket = 0 then bucket_0[i] = vars_of_interest[i];
else if vars_of_interest[i] < bucket_0[i] / 100 then call missing(vname(vars_of_interest[i]))
end;
run;
You need a way to remember the values from the first row (or perhaps from the row where BUCKET=0?) so that you can then compare the value from the first row to the current value. A temporary ARRAY is an easy way to do that.
So assuming that BUCKET is always the first numeric variable in your data then you can just do something like this.
data want ;
set my_data;
array x _numeric_;
array y (1000) _temporary_;
do i=2 to dim(x);
if bucket=0 then y(i)=x(i);
else if x(i) < y(i) then x(i)=.;
end;
drop i;
run;
If BUCKET is not the first variable then you could add retain bucket; before the set statement to force it to be the first. Or change the first array statement to list the specific variables you want to process, just remember to change the lower bound on the DO loop.
If you have more than a thousand variables then increase the dimension of the temporary array.
I want to output the last observation in variable which is an integer sequence in a sas data set.
I have this data set:
data have;
input seq var;
datalines;
1 7
2 6
3 3
1 1
2 4
1 8
2 9
3 1
4 8
;
run;
I would like to achieve the following:
seq var
3 3
2 4
4 8
I have thoroughly searched for my answer online but couldn't find anything.
You can use a look-ahead technique. This is one of many ways to write it.
data last;
set have end=eof;
if not eof then set have(firstobs=2 keep=seq rename=(seq=nseq));
if nseq eq 1 or eof then output;
drop nseq;
run;
Just to give an indication of the slickness of the look-ahead approach - you can do the same thing with lag, but it takes nearly twice as many lines of code:
data want(drop=prev_:);
set have end = eof;
prev_seq = lag(seq);
prev_var = lag(var);
if seq < prev_seq then do;
seq = prev_seq;
var = prev_var;
end;
if eof or seq = prev_seq;
run;