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

Hello,

 

Please, I have a data set in longitudinal format as show below. I want to create additional column CENST in which the values of CENS is shift one row up and the values after first none-zero value is missing for each subject.

 

data have;

input subjid age cens censt;

datalines;

SUBIJD

age

cens

100-001

50

0

100-001

50

0

100-001

50

1

100-001

50

1

100-001

50

1

100-002

35

0

100-002

35

0

100-002

35

0

100-002

35

1

100-002

35

1

100-002

35

1

 

Data want;

 

SUBIJD

age

cens

censt

100-001

50

0

0

100-001

50

0

1

100-001

50

1

.

100-001

50

1

.

100-001

50

1

.

100-002

35

0

0

100-002

35

0

0

100-002

35

0

1

100-002

35

1

.

100-002

35

1

.

100-002

35

1

.

 

;run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;

input subjid $ age cens ;

datalines;
100-001

50

0

100-001

50

0

100-001

50

1

100-001

50

1

100-001

50

1

100-002

35

0

100-002

35

0

100-002

35

0

100-002

35

1

100-002

35

1

100-002

35

1
;

data want;
 do until(last.subjid);
  set have;
  by subjid cens notsorted;
  censt=cens;
  if not cens and last.cens then censt=1;
  else if cens then call missing(censt);
  output;
 end;
run;

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

And please post WORKING data step code into a window opened with the "little running man"; only then will we be able to recreate your dataset without more work than a copy/paste and submit. Your code as posted will not run.

novinosrin
Tourmaline | Level 20

data have;

input subjid $ age cens ;

datalines;
100-001

50

0

100-001

50

0

100-001

50

1

100-001

50

1

100-001

50

1

100-002

35

0

100-002

35

0

100-002

35

0

100-002

35

1

100-002

35

1

100-002

35

1
;

data want;
 do until(last.subjid);
  set have;
  by subjid cens notsorted;
  censt=cens;
  if not cens and last.cens then censt=1;
  else if cens then call missing(censt);
  output;
 end;
run;
mkeintz
PROC Star

The is a typical   set/by plus self-merge with offset task:

 

data want (drop=onefound);
  retain onefound 'N';

  set have (keep=subjid);
  by subjid;
  if first.subjid then onefound='N';
  merge have
        have (firstobs=2 keep=cens rename=(cens=censt));

  if last.subjid=1 or onefound='Y' then call missing(censt);
  if censt=1 then onefound='Y';
run;
--------------------------
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

--------------------------
SWEETSAS
Obsidian | Level 7

CENS remains as it. CENST is obtained from CENS by shifting one record upward and then declaring missing all 1s after the first 1. One can think about it this way. Within each ID, data represent values of measurement for different visit. CENS reflects the point an event occurred. By shifting CENS one record up, the analysis can use values of other variables just before the event occured because the patient is at risk prior to the event captured by CENS.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 764 views
  • 0 likes
  • 4 in conversation