10-18-2021
PetePatel
Quartz | Level 8
Member since
07-19-2017
- 77 Posts
- 33 Likes Given
- 0 Solutions
- 0 Likes Received
-
Latest posts by PetePatel
Subject Views Posted 1363 10-18-2021 12:05 PM 1401 10-18-2021 11:33 AM 691 06-25-2021 04:13 AM 772 06-01-2021 11:08 AM 1222 05-27-2021 04:33 AM 1582 10-13-2020 03:44 AM 635 08-06-2020 04:53 AM 1394 08-05-2020 12:53 PM 1418 08-05-2020 12:38 PM 1426 08-05-2020 12:31 PM -
Activity Feed for PetePatel
- Posted Re: SAS Macro on SAS Programming. 10-18-2021 12:05 PM
- Liked Re: SAS Macro for PaigeMiller. 10-18-2021 12:05 PM
- Posted SAS Macro on SAS Programming. 10-18-2021 11:33 AM
- Liked Re: Retaining a flag value by id for Kurt_Bremser. 06-25-2021 09:33 AM
- Posted Retaining a flag value by id on SAS Programming. 06-25-2021 04:13 AM
- Liked Re: Flagging specific cases by ID for FreelanceReinh. 06-02-2021 03:42 AM
- Posted Flagging specific cases by ID on SAS Programming. 06-01-2021 11:08 AM
- Liked Re: Counting instances of 1 to 0 for mkeintz. 06-01-2021 10:57 AM
- Posted Counting instances of 1 to 0 on SAS Programming. 05-27-2021 04:33 AM
- Posted Transpose on SAS Programming. 10-13-2020 03:44 AM
- Posted SAS counter reset on SAS Programming. 08-06-2020 04:53 AM
- Posted Re: Counter flag for historic months on SAS Programming. 08-05-2020 12:53 PM
- Posted Re: Counter flag for historic months on SAS Programming. 08-05-2020 12:38 PM
- Posted Re: Counter flag for historic months on SAS Programming. 08-05-2020 12:31 PM
- Posted Counter flag for historic months on SAS Programming. 08-05-2020 12:00 PM
- Posted Re: SQL merge to SAS merge (Proc SQL) on SAS Programming. 05-20-2020 12:24 PM
- Posted SQL merge to SAS merge (Proc SQL) on SAS Programming. 05-20-2020 11:57 AM
- Posted Re: Sumproduct and Weighting on SAS Programming. 03-30-2020 11:28 AM
- Posted Sumproduct and Weighting on SAS Programming. 03-30-2020 11:09 AM
- Posted Re: Copy first row value for all rows on SAS Programming. 03-30-2020 05:42 AM
-
Posts I Liked
Subject Likes Author Latest Post 1 1 1 2 1
10-18-2021
12:10 PM
@PetePatel wrote:
Thanks PaigeMiller. The variables need to be dropped within the set statement as they are causing issues when trying to set together. Your second macro code is what I was after.
If the particular issue(s) were mismatched variable types or lengths of character variables it may be time to address how the data sets are created.
If the issue was just data set size that's another story.
... View more
06-25-2021
04:37 AM
1 Like
data have;
input id $ month :monyy6. signal;
format month yymmd7.;
datalines;
1 Jan-20 0
1 Feb-20 0
1 Mar-20 1
1 Apr-20 1
1 May-20 2
1 Jun-20 2
1 Jul-20 3
1 Aug-20 3
1 Sep-20 3
1 Oct-20 3
1 Nov-20 3
1 Dec-20 4
2 Jun-20 0
2 Jul-20 4
2 Aug-20 4
2 Sep-20 4
2 Oct-20 2
2 Nov-20 0
2 Dec-20 1
2 Jan-21 1
2 Feb-21 2
2 Mar-21 4
2 Apr-21 4
2 May-21 4
2 Jun-21 1
;
data want;
set have;
by id;
retain sig_count;
if first.id then sig_count = 0;
if signal = 2
then sig_count = 4;
else sig_count = max(0,sig_count - 1);
if sig_count in (3,2)
then signal_2 = 2;
else signal_2 = signal;
if sig_count ne 0
then signal_3 = 2;
else signal_3 = signal;
drop sig_count;
run;
proc print data=want noobs;
run;
Result:
id month signal signal_2 signal_3
1 2020-01 0 0 0
1 2020-02 0 0 0
1 2020-03 1 1 1
1 2020-04 1 1 1
1 2020-05 2 2 2
1 2020-06 2 2 2
1 2020-07 3 2 2
1 2020-08 3 2 2
1 2020-09 3 3 2
1 2020-10 3 3 3
1 2020-11 3 3 3
1 2020-12 4 4 4
2 2020-06 0 0 0
2 2020-07 4 4 4
2 2020-08 4 4 4
2 2020-09 4 4 4
2 2020-10 2 2 2
2 2020-11 0 2 2
2 2020-12 1 2 2
2 2021-01 1 1 2
2 2021-02 2 2 2
2 2021-03 4 2 2
2 2021-04 4 2 2
2 2021-05 4 4 2
2 2021-06 1 1 1
Note how I presented source data in a data step with datalines; please do so yourself in the future, as it makes helping you much easier. Help us to help you.
... View more
06-02-2021
09:10 AM
data have;
input ID Month :monyy. _1p_flag;
format Month monyy.;
cards;
1 Mar-12 0
1 Apr-12 0
1 May-12 1
1 Jun-12 1
1 Jul-12 0
1 Aug-12 0
1 Sep-12 1
1 Oct-12 0
2 Apr-12 1
2 May-12 0
2 Jun-12 0
2 Jul-12 1
2 Aug-12 0
2 Sep-12 0
2 Oct-12 0
2 Nov-12 1
2 Dec-12 0
2 Jan-13 1
2 Feb-13 1
;
data want;
retain has_one n 0;
do i=1 by 1 until(last._1p_flag);
set have;
by id _1p_flag notsorted;
if first.id then do;has_one=0;n=0;end;
if first._1p_flag and _1p_flag=1 and has_one=1 then do; _1_to_0_to_1=1;months_between=n+1;end;
output;
call missing(_1_to_0_to_1,months_between);
end;
n=i;
if _1p_flag=1 then has_one=1;
drop n i has_one;
run;
... View more
05-27-2021
08:06 AM
2 Likes
This is a good task to wax a little didactic about using the queue-based nature of the lag function:
data want;
set have (in=firstpass)
have (in=secondpass);
by id;
if firstpass then sum_one_to_zero + (lag(flag)=1 and flag=0);
if first.id then sum_one_to_zero=0;
if secondpass;
if lag(flag)=1 and flag=0 then one_to_zero=1;
if lag(id)^=id then one_to_zero=.;
run;
The statement:
if firstpass then sum_one_to_zero + (lag(flag)=1 and flag=0);
compares the current flag to the preceding flag, building a total of transitions from 1 to 0. Because the lag function is in the then clause, it is applied only for firstpass cases - secondpass cases never impact the queue underlying the lag function.
To avoid results from the preceding id contaminating the current id, the sum is reset to zero at the start of each ID.
if first.id then sum_one_to_zero=0;
The third use of the lag function is more subtle. The statement
if lag(id)^=id then one_to_zero=.;
appear to test whether the record-in-hand is the start of an id. So why not just use
if first.id then one_to_zero=.; /*Do not use this for secondpass*/
Because this part of the program only deals with second_pass observations, while the first.id condition only exists for firstpass observations. So you basically have to realize that this part of the program is only processing groups of secondpass observations.
... View more
08-06-2020
05:21 AM
Easily done with a retained variable that is set or incremented on conditions:
data have;
input ID Mon $ Flag Need;
datalines;
1 Jan-06 0 0
1 Feb-06 0 0
1 Mar-06 0 0
1 Apr-06 0 0
1 May-06 0 0
1 Jun-06 0 0
1 Jul-06 0.7 1
1 Aug-06 0 2
1 Sep-06 1 1
1 Oct-06 1.3 1
1 Nov-06 0 2
1 Dec-06 0 3
2 Feb-14 0 0
2 Mar-14 0 0
2 Apr-14 0 0
2 May-14 0 0
2 Jun-14 0.6 1
2 Jul-14 0 2
2 Aug-14 0 3
2 Sep-14 0 4
2 Oct-14 0 5
2 Nov-14 2 1
3 Aug-18 0 0
3 Sep-18 0 0
3 Oct-18 0 0
3 Nov-18 0 0
3 Dec-18 0 0
;
data want;
set have (rename=(need=want_need));
by id;
if first.id then need = 0;
if flag ne 0 then need = 1;
else if need ne 0 then need + 1;
run;
The retain is done automatically because of the use of the SUM Statement (need + 1).
... View more
08-05-2020
12:53 PM
Thanks @PaigeMiller the car_flag3 variable works perfectly! MSC should continue counting as it's a months since car>0. For example, if car>0 in Feb-14 for a particular ID then MSC=1 in Mar-14 MSC=2 if car=0 Apr-14 MSC=3 if car=0 In May-14 if car>0 again for that ID then the MSC counter starts from 1 again. Can that be incorporated into your code?
... View more
05-20-2020
12:35 PM
What does ISNULL() do in your second filter? You have to convert all your functions to SAS functions or explicit SQL Pass through.
... View more
04-02-2020
07:15 AM
1 Like
Did you run the code and check WANT table ? Var NLevels Name 19 Sex 2 Age 6 Height 17 Weight 15
... View more
03-30-2020
05:42 AM
Thanks! I tried retain with first.date to cover myself but this works fine.
... View more
03-05-2020
12:21 PM
Yes sorry should have mentioned SAS EG. Works perfectly now thanks!
... View more
02-20-2020
08:49 AM
Thank you novinosrin, I didn't know you could use a groupformat in such a way.
... View more
02-12-2020
10:49 AM
Here is one approach to retrieve the last five variables of your dataset:
proc contents data=have out=want (keep=name varnum where=(name not in ('Date', 'Time', 'Month'))) noprint;
run;
data list;
if _n_=1 then do;
declare hash h (dataset:'want', ordered:'d');
h.definekey('VARNUM');
h.definedata('VARNUM','NAME');
h.definedone();
declare hiter C('h');
end;
set want;
C.first();
output;
do i=1 to 4;
C.next();
output;
end;
stop;
run;
... View more
02-11-2020
05:30 AM
1 Like
Anytime, glad you found your answer 🙂
... View more