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

data ONE;
input @1 id @6 sex $1. @9 dob mmddyy8. salary;
format dob mmddyy8.;
datalines;
1 M 10/21/46 70000
2 F 11/01/55 68000
7 M 01/27/59 47000
;
run;

data TWO;
input @1id @6 sex $1. @9 dob mmddyy8. @19 salary @30 taxrate @39withhold;
format dob mmddyy8.;
datalines;
3 F 01/01/33 78000 .36 23000
5 M 03/07/29 37000 .25 9000
;
run;

data THREE;
input @1 id @6 sex $1. @9 dob mmddyy8. @19salary @29 height @39 weight;
format dob mmddyy8.;
datalines;
4 M 10/23/49 65000 68 158
6 F 07/04/65 55000 74 202
;
run;
data ALL (keep = id dob salary);
merge ONE TWO THREE;
by id;
proc print data = ALL;
title2'ALL';
run;

Using the three datasets , write a program to create a new SAS dataset EMP for those people who born on or before Jan 1, 1960, and who earn $50,000 or more per year.


* Change dataset TWO, variable ID to IDNUM

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@RAVI2000 wrote:

I have created the dob with mmddyy8. format. 


The code you pasted above to create the data sets may not be the code you executed. The main message windows on this forum reformat text. So the data steps shown above do not execute. Mainly because the @9 on the input statement does not align with the columns in the date after pasting in the message windows and so the id was "reading" 6 columns as numeric and failing because of the space between the single character and the Ses, date is read from the wrong columns and doesn't match the informat because column 9 is the middle of the day part of the value.

Code should be pasted into a code box opened with either the </> or "running man" icon to preserve spaces and such. Also tabs count differently than spaces and can be problematic between different systems.

When I modify the pasted code above to and only use the first set

data ONE;
input @1 id $ sex $1. dob :mmddyy8. salary;
format dob mmddyy8.;
datalines;
1 M 10/21/46 70000
2 F 11/01/55 68000
7 M 01/27/59 47000
;
run;
data EMP;
  set ONE ;
  if dob le '01JAN1960'd and salary ge 50000;
run;

This extracts the two records with the income over 50000.

 

Here is the Log I got from running the code for data one as shown above:

165  data ONE;
166  input @1 id @6 sex $1. @9 dob mmddyy8. salary;
167  format dob mmddyy8.;
168  datalines;

NOTE: Invalid data for dob in line 169 9-16.
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+--
169        1 M 10/21/46 70000
id=1 sex=0 dob=. salary=0 _ERROR_=1 _N_=1
NOTE: Invalid data for dob in line 170 9-16.
170        2 F 11/01/55 68000
id=2 sex=1 dob=. salary=0 _ERROR_=1 _N_=2
NOTE: Invalid data for dob in line 171 9-16.
171        7 M 01/27/59 47000
id=7 sex=1 dob=. salary=0 _ERROR_=1 _N_=3
NOTE: The data set WORK.ONE has 3 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

If you look carefully you see that the value for SEX on the first line is 0 not the M expected. That is because the 6th character (after pasting in the message window) is the 0 form the 10/21/46.


View solution in original post

9 REPLIES 9
ballardw
Super User

If the question is how to compare a date valued variable to a specific date then you use the value quoted in date9 format followed by d.

 

if dob le '01JAN1960'd and salary ge 50000;

 

RAVI2000
Lapis Lazuli | Level 10

when i try doing so,  i am getting blank obs in my o/p.

data EMP;
  set ONE TWO(RENAME =(ID = IDNUM)) THREE;
  if dob le '01JAN1960'd and salary ge 50000;
proc print data = EMP;
run;

Capture.PNG 

RAVI2000
Lapis Lazuli | Level 10
people who born on or before Jan 1, 1960, and who earn $50,000 or more per year. */

data EMP;
  set ONE TWO(RENAME =(ID = IDNUM)) THREE;
  if dob le '01JAN1960'd and salary ge 50000;
proc print data = EMP;
run;

Capture.PNGi want to create a dataset who are born on or before jan 1, 1960 with income gt 50000. but it gives me blanks in obs.

 

 

Jagadishkatam
Amethyst | Level 16

Is DOB variable a character variable or numeric variable.

Thanks,
Jag
RAVI2000
Lapis Lazuli | Level 10

I have created the dob with mmddyy8. format. 

ballardw
Super User

@RAVI2000 wrote:

I have created the dob with mmddyy8. format. 


The code you pasted above to create the data sets may not be the code you executed. The main message windows on this forum reformat text. So the data steps shown above do not execute. Mainly because the @9 on the input statement does not align with the columns in the date after pasting in the message windows and so the id was "reading" 6 columns as numeric and failing because of the space between the single character and the Ses, date is read from the wrong columns and doesn't match the informat because column 9 is the middle of the day part of the value.

Code should be pasted into a code box opened with either the </> or "running man" icon to preserve spaces and such. Also tabs count differently than spaces and can be problematic between different systems.

When I modify the pasted code above to and only use the first set

data ONE;
input @1 id $ sex $1. dob :mmddyy8. salary;
format dob mmddyy8.;
datalines;
1 M 10/21/46 70000
2 F 11/01/55 68000
7 M 01/27/59 47000
;
run;
data EMP;
  set ONE ;
  if dob le '01JAN1960'd and salary ge 50000;
run;

This extracts the two records with the income over 50000.

 

Here is the Log I got from running the code for data one as shown above:

165  data ONE;
166  input @1 id @6 sex $1. @9 dob mmddyy8. salary;
167  format dob mmddyy8.;
168  datalines;

NOTE: Invalid data for dob in line 169 9-16.
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+--
169        1 M 10/21/46 70000
id=1 sex=0 dob=. salary=0 _ERROR_=1 _N_=1
NOTE: Invalid data for dob in line 170 9-16.
170        2 F 11/01/55 68000
id=2 sex=1 dob=. salary=0 _ERROR_=1 _N_=2
NOTE: Invalid data for dob in line 171 9-16.
171        7 M 01/27/59 47000
id=7 sex=1 dob=. salary=0 _ERROR_=1 _N_=3
NOTE: The data set WORK.ONE has 3 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

If you look carefully you see that the value for SEX on the first line is 0 not the M expected. That is because the 6th character (after pasting in the message window) is the 0 form the 10/21/46.


RAVI2000
Lapis Lazuli | Level 10

why did you gave variable id as character ?

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!

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
  • 9 replies
  • 857 views
  • 1 like
  • 4 in conversation