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

I have the following data below. I would like to choose the obs where
first_year eq 1. I would like my data set to look like the table below
could someone assist me with this?

 

data data1;
infile datalines delimiter = ',';
input customerid $ year regdate:mmddyy10. completedate:mmddyy10. first_ssn
last_ssn first_year last_year;
format regdate completedate mmddyy10.;
datalines;
100, 2019,04/30/2019, 01/24/2018, 1,0,1,0
100,2018,12/31/2018,12/16/2019, 0,0,0,0
100,2018,11/30/2018,12/16/2019, 0,0,0,1
100,2019, 08/31/2019,12/16/2019,0,0,1,0
;
run;

customerid year regdate completedate first_ssn last_ssn first_year last_year
100 2019 4/30/2019 1/24/2018 1 0 1 0
100 2019 8/31/2019 12/16/2019 0 0 1 0
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@luvscandy27 wrote:

 

The dataset above was created using first. last. I was trying to remove the obs I don't need using first.last.
I tried the code below but it does not keep all the obs where first_year eq 1:

 

data want;
set datal;
by ssn first_year;
if first.first_year;
run;


The IF FIRST.<variable name> syntax returns the first row of a sorted set and not all rows with a specific value. 

 

Try one of the options below for what you want.

data data1;
  infile datalines delimiter = ',';
  input customerid $ year regdate:mmddyy10. completedate:mmddyy10. first_ssn
    last_ssn first_year last_year;
  format regdate completedate mmddyy10.;
  datalines;
100, 2019,04/30/2019, 01/24/2018, 1,0,1,0
100,2018,12/31/2018,12/16/2019, 0,0,0,0
100,2018,11/30/2018,12/16/2019, 0,0,0,1
100,2019, 08/31/2019,12/16/2019,0,0,1,0
;
run;

proc print data=data1(where=(first_year=1));
run;

data want1;
  set data1(where=(first_year=1));
run;

data want2;
  set data1;
  where first_year=1;
run;

data want3;
  set data1;
  if first_year=1;
run;

View solution in original post

3 REPLIES 3
ballardw
Super User

Generic form to create a subset of data based on an existing variable for a given value:

 

Data want;

   set have;

   where variable=1;

run;

 

You may not even need to create a new data set as you can use data set options such as

 

Proc print data=have (where=(variable=1));

run;

 

to select the records to use as needed.

luvscandy27
Quartz | Level 8

 

The dataset above was created using first. last. I was trying to remove the obs I don't need using first.last.
I tried the code below but it does not keep all the obs where first_year eq 1:

 

data want;
set datal;
by ssn first_year;
if first.first_year;
run;

Patrick
Opal | Level 21

@luvscandy27 wrote:

 

The dataset above was created using first. last. I was trying to remove the obs I don't need using first.last.
I tried the code below but it does not keep all the obs where first_year eq 1:

 

data want;
set datal;
by ssn first_year;
if first.first_year;
run;


The IF FIRST.<variable name> syntax returns the first row of a sorted set and not all rows with a specific value. 

 

Try one of the options below for what you want.

data data1;
  infile datalines delimiter = ',';
  input customerid $ year regdate:mmddyy10. completedate:mmddyy10. first_ssn
    last_ssn first_year last_year;
  format regdate completedate mmddyy10.;
  datalines;
100, 2019,04/30/2019, 01/24/2018, 1,0,1,0
100,2018,12/31/2018,12/16/2019, 0,0,0,0
100,2018,11/30/2018,12/16/2019, 0,0,0,1
100,2019, 08/31/2019,12/16/2019,0,0,1,0
;
run;

proc print data=data1(where=(first_year=1));
run;

data want1;
  set data1(where=(first_year=1));
run;

data want2;
  set data1;
  where first_year=1;
run;

data want3;
  set data1;
  if first_year=1;
run;

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
  • 3 replies
  • 476 views
  • 1 like
  • 3 in conversation