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
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.
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;
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;
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.
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.
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.