Hi,
i am very new to SAS , and am stuck with some basic concept like using arbitrary variable for merging data sets > Could some one please explain the logic for the same.
Data Num2;
Set Num;
i = 1; *Create an arbitrary variable for merging purpose;
Run;
proc means data=num2 noprint;
Var Rnumber;
By i;
Output out=stat mean=mean std=std;
run;
Data Zscore;
Merge Num2 Stat;
By i;
Zscore = (Rnumber - Mean)/std;
Run;
Apparently, this program was written by someone who is just slightly more advanced than you are. The real problem is to get the MEAN and STD appended to each observation, to support Z score calculations. This can be done in several ways, none of which involve creating an arbitrary variable for merging. Here is the way that most closely resembles the existing program. Begin with:
proc means data=num noprint;
var Rnumber;
output out=stat mean=mean std=std;
run;
At this point, you might want to examine the results since you could use some familiarity with what the program does. Then continue with:
data zscore;
if _n_=1 then set stat (keep=mean std);
set num;
zscore = (Rnumber - mean) / std;
run;
There are several other ways to accomplish this task (SQL, macro language, even a SAS procedure that standardizes variables). But this is the way that is closest to your existing program.
Apparently, this program was written by someone who is just slightly more advanced than you are. The real problem is to get the MEAN and STD appended to each observation, to support Z score calculations. This can be done in several ways, none of which involve creating an arbitrary variable for merging. Here is the way that most closely resembles the existing program. Begin with:
proc means data=num noprint;
var Rnumber;
output out=stat mean=mean std=std;
run;
At this point, you might want to examine the results since you could use some familiarity with what the program does. Then continue with:
data zscore;
if _n_=1 then set stat (keep=mean std);
set num;
zscore = (Rnumber - mean) / std;
run;
There are several other ways to accomplish this task (SQL, macro language, even a SAS procedure that standardizes variables). But this is the way that is closest to your existing program.
The concept of the entire program is this.
You need to understand (and possibly already understand) how MERGE works in SAS. MERGE requires a common variable (or variables) used to match on, to combine the variables from two data sets onto the same observation.
These two data sets ... the original data, plus the results from PROC MEANS ... do not have a common variable. So the program makes one up. It calls the variable "i" and gives it an arbitrary value so it will match all the time. It doesn't matter what value is assigned to "i" (whether it is "1" or "abc") as long as it matches between the two data sets. That allows a MERGE step to take place.
Normally you would sort your data sets first, before using MERGE. In this case, there is no need to sort, since the variable takes on only one value. For the purposes of MERGE, the data sets are already sorted.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.