BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Lapis Lazuli | Level 10

Dear all, 

suppose to have the following: 

 

 

data DB;
  input ID :$20. Admission :date09. Discharge :date09. Age_classStart Age_classEnd; 
  format Admission date9. Discharge date9.;
cards;
0001 13JAN2015 20JAN2015 5  8
0001 21FEB2015 31DEC2015 5  8
0001 01MAR2018 30SEP2018 6  8
0001 01JAN2019 31DEC2019 6  8
0002 01JAN2015 31DEC2015 1  3
0002 01JAN2019 31OCT2019 3  3
0003 08FEB2014 10MAR2014 6  9
0003 16JUN2015 13JUL2015 6  9
0004 04MAY2016 10MAY2016 4  6
0004 13SEP2017 15NOV2017 4  6
0004 09DEC2018 31DEC2018 5  6
;

Is there a way to get the following? 

 

data DB1;
  input ID :$20. Admission :date09. Discharge :date09. Age_class1 Age_class2 Age_class3 Age_class4 Age_class5 Age_class6 Age_class7 Age_class8 Age_class9; 
  format Admission date9. Discharge date9.;
cards;
0001 13JAN2015 20JAN2015 . . . . 1 1 1 1 .
0001 21FEB2015 31DEC2015 . . . . 1 1 1 1 .
0001 01MAR2018 30SEP2018 . . . . . 1 1 1 .
0001 01JAN2019 31DEC2019 . . . . . 1 1 1 .
0002 01JAN2015 31DEC2015 1 1 1 . . . . . .
0002 01JAN2019 31OCT2019 . . 1 . . . . . .
0003 08FEB2014 10MAR2014 . . . . . 1 1 1 1
0003 16JUN2015 13JUL2015 . . . . . 1 1 1 1
0004 04MAY2016 10MAY2016 . . . 1 1 1 . . .
0004 13SEP2017 15NOV2017 . . . 1 1 1 . . .
0004 09DEC2018 31DEC2018 . . . . 1 1 . . .
;

In other word given the Age_classStart and Age_classEnd range, if out of the range set missing otherwise 1. 

 

Thank you in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Zard
SAS Employee

You could use an array:

data DB;
  set DB;
  array AgeClass{9} Age_class1 - Age_class9;
  do i=1 to 9;
    if Age_classStart LE i LE Age_classEnd then AgeClass{i}=1;
      else AgeClass{i}=.;
  end;
  output;
  drop i;
run;

This would give you the missing values and 1's. If you wanted 1's and 0's, change the DO loop to

do i=1 to 9;
  AgeClass{i}= Age_classStart LE i LE Age_classEnd;
end;

 

View solution in original post

1 REPLY 1
Zard
SAS Employee

You could use an array:

data DB;
  set DB;
  array AgeClass{9} Age_class1 - Age_class9;
  do i=1 to 9;
    if Age_classStart LE i LE Age_classEnd then AgeClass{i}=1;
      else AgeClass{i}=.;
  end;
  output;
  drop i;
run;

This would give you the missing values and 1's. If you wanted 1's and 0's, change the DO loop to

do i=1 to 9;
  AgeClass{i}= Age_classStart LE i LE Age_classEnd;
end;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 980 views
  • 1 like
  • 2 in conversation