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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

4 REPLIES 4
Astounding
PROC Star

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.

jjoy4891
Fluorite | Level 6
Hi,
Its definitely written by someone else.. I got this code from sascrunch training - data analysis section.- what i meant is that i didnt understand the concept of using 'i' . seen the situation in other training sections also. Could you please elaborate on this.
many thanks
Astounding
PROC Star

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.

jjoy4891
Fluorite | Level 6
Thank you so much for the explanation.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

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.

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
  • 847 views
  • 2 likes
  • 2 in conversation