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

Q1: How in sas do i do what would be in spss a match files with a table? so, suppose

File A with variables id w e r t
1 2 3 4 5
1 8 4 5 9
1 9 3 4 5
2 8 3 1 1
2 9 3 4 5

file B with variables id kk
1 99
2 56

to get id w e r t kk
1 2 3 4 5 99
1 8 4 5 9 99
1 9 3 4 5 99
2 8 3 1 1 56
2 9 3 4 5 56

in spss
match file file=a/table=b/by id.

in sas?????

 

Q2: What is the sas record counter variable? In spss it is $casenum. What is it in sas?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

When you MERGE the observations with the same values of the BY variables are combined. (If one or more of the datasets have multiple observations for the same set of BY variable values then the observations are matched in order and any dataset that runs out of observations for that group will just keep the values the last observation in the group had).

 

So in your example:

NOTE: There were 9145 observations read from the data set WORK.STACKY3.
NOTE: There were 36 observations read from the data set WORK.COHORTS2.
NOTE: The data set WORK.STACK4 has 9163 observations and 753 variables.

If none of the YBW values matched then the number of observations written would be the sum of the number read from each input.  Since 9,163 is less than 9,145 plus 36 there are some value of YBW that did match.   Since 9,163 is more than 9,145 there are some observations where the value of YBM did not match (or where that value appeared more than once in WORK.COHORTS2)

 

To find out exactly which they are you can use the IN= dataset options on the input datasets to create temporary variables that will be true when that dataset is contributing data.

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20
data a;
input id w e r t;
datalines;
1 2 3 4 5
1 8 4 5 9
1 9 3 4 5
2 8 3 1 1
2 9 3 4 5
;

data b;
input id kk;
datalines;
1 99
2 56
;

data want;
   merge a b;
   by id;
run;
Kurt_Bremser
Super User

The number of observations in a dataset can be retrieved from DICTIONARY.TABLES (SQL) or SASHELP.VTABLE (data or other procedure steps).

In a data step, you can use the NOBS= option in a SET statement to define a variable that holds this number.

If you use a simple data step (which only reads one observation per iteration) you can use _N_ to retrieve the number of the current observation.

 

For the match/merge, use either the data step MERGE as shown by @PeterClemmensen , or use SQL:

proc sql;
create table want as
  select a.*, b.kk
  from a left join b
  on a.id = b.id
;
quit;
emaguin
Quartz | Level 8
On Q1. I thought of merge and tried it.
data stack4; merge stacky3 cohorts2; by ybw; run;

NOTE: There were 9145 observations read from the data set WORK.STACKY3.
NOTE: There were 36 observations read from the data set WORK.COHORTS2.
NOTE: The data set WORK.STACK4 has 9163 observations and 753 variables.

Continuing with my example
File a
1 2 3 4 5
1 8 4 5 9
1 9 3 4 5
2 8 3 1 1
2 9 3 4 5

File b
1 99
2 56

What I got
1 2 3 4 5 99
1 8 4 5 9 99
1 9 3 4 5 99
2 8 3 1 1 56
2 9 3 4 5 56
. . . . . 99
. . . . . 56

I ran your example and it works as I expected it to, i.e., correctly.
My result suggests a blank record in the stacky3 dataset; however, looking frequencies on a variable there is no missing data. Thus, I assume no blank record. I also checked frequencies in both files on my by variable and no missing data there either. According to the notes everything worked perfectly except it didn't. Suggestions about how to debug the problem? Thanks.


On Q2. Thanks. So, then I could say something like this?
if (_n_ ge 34 and _n_ le 44) then
put something somewhere (skip over the put part; I'm concerned about the if part)
Tom
Super User Tom
Super User

When you MERGE the observations with the same values of the BY variables are combined. (If one or more of the datasets have multiple observations for the same set of BY variable values then the observations are matched in order and any dataset that runs out of observations for that group will just keep the values the last observation in the group had).

 

So in your example:

NOTE: There were 9145 observations read from the data set WORK.STACKY3.
NOTE: There were 36 observations read from the data set WORK.COHORTS2.
NOTE: The data set WORK.STACK4 has 9163 observations and 753 variables.

If none of the YBW values matched then the number of observations written would be the sum of the number read from each input.  Since 9,163 is less than 9,145 plus 36 there are some value of YBW that did match.   Since 9,163 is more than 9,145 there are some observations where the value of YBM did not match (or where that value appeared more than once in WORK.COHORTS2)

 

To find out exactly which they are you can use the IN= dataset options on the input datasets to create temporary variables that will be true when that dataset is contributing data.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 368 views
  • 0 likes
  • 4 in conversation