Hi Everyone,
I have a data as below (with many numeric variables) and at the end, I only want to pick record with CHECK^=.
for that record, for each numeric variable (v1, v2.....), I want to add all values from after the last time Check^=.
so row 1 will be kept
row 4 will be kept and the Check, V1, V2 as below:
5 (2+1) (3)
last row will be kept and the Check, V1, V2 as below:
9 1 (2+1)
Can you please help me with that?
Thank you so much
HHCFX
data have;
set check v1 v2;
datalines;
1 3 4
. 2 .
. 1 3
5 . .
. 1 2
9 . 1
;run;
I requested a working DATA HAVE code, simply as a good model for others posting questions. In your case, simply using INPUT instead of SET.
I understand you only want to output the records with check^=., and V1 and V2 modified as described:
data have;
input check v1 v2;
datalines;
1 3 4
. 2 .
. 1 3
5 . .
. 1 2
9 . 1
run;
data want (drop=_:);
set have;
_v1+v1;
_v2+v2;
if check^=.;
v1=_v1;
v2=_v2;
call missing(of _:);
run;
If you want ALL records, then change "if check^=.;" with "if check ^=. then do;", and put an "end;" statement after the call missing.
I see you have over 500 posts to sas communities. Could you please provide the sample data with working data step code?
I totally stuck with that problem.
I am not sure what info you need since I have the sample data at the end. Also the output data is explained in the body.
I requested a working DATA HAVE code, simply as a good model for others posting questions. In your case, simply using INPUT instead of SET.
I understand you only want to output the records with check^=., and V1 and V2 modified as described:
data have;
input check v1 v2;
datalines;
1 3 4
. 2 .
. 1 3
5 . .
. 1 2
9 . 1
run;
data want (drop=_:);
set have;
_v1+v1;
_v2+v2;
if check^=.;
v1=_v1;
v2=_v2;
call missing(of _:);
run;
If you want ALL records, then change "if check^=.;" with "if check ^=. then do;", and put an "end;" statement after the call missing.
Thanks a lot for your help.
That works perfectly.
HHCFX
By the way, is there any way to do that for all _numeric_ variables since I have 20+ variables to perform that process?
And when I change my file, the variables are not the same.
Yes, you can do this for all numeric variables, but instead of summing statements, like
_v1+v1;
_v2+v2;
you can use a temporary array.
data have;
input check v1 v2;
datalines;
1 3 4
. 2 .
. 1 3
5 . .
. 1 2
9 . 1
run;
data want (drop=_:);
set have;
array tmps {30} _temporary_;
array nbrs {*} _numeric_;
do _i=1 to dim(nbrs);
tmps{_i}+nbrs{_i};
end;
if check^=.;
do _i=1 to dim(nbrs);
nbrs{_i}=tmps{_i};
tmps{_i}=.;
end;
run;
The only caveat is to declare the array tmps to have at least as many elements (I used 30 above) as the array nbrs. It can be larger, but not smaller. You could declare _tmps to be of size 1,000 and it wouldn't hurt.
Thank you so much for your code.
HHC
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.