BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Rhodochrosite | Level 12

 

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;

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

7 REPLIES 7
mkeintz
PROC Star

I see you have over 500 posts to sas communities.  Could you please provide the sample data with working data step code?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
hhchenfx
Rhodochrosite | Level 12

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.

 

mkeintz
PROC Star

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.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
hhchenfx
Rhodochrosite | Level 12

Thanks a lot for your help.

That works perfectly.

HHCFX

hhchenfx
Rhodochrosite | Level 12

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.

mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
hhchenfx
Rhodochrosite | Level 12

Thank you so much for your code.

HHC

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1157 views
  • 0 likes
  • 2 in conversation