Hi, I am sure these are fairly easy question. But I am working with some code and there are couple parts I cannot seem to understand.
This data step for example, has a line of code with (length e $10). The variable e is not in either of the datasets ( m and y) being merged. So is this just a way of creating a new variable? And how does one decide on the length($) of the new variable.
data X;
length e $10;
merge m (in=a) y;
by ID;
if a;
run;
And then for this code below, what is its purpose?
proc sql noprint; select count (*) into: number from final where &var=&num1; quit;
%put Count=&number;
Thank you, any help is greatly appreciated it!
@kmardinian wrote:
Hi, I am sure these are fairly easy question. But I am working with some code and there are couple parts I cannot seem to understand.
This data step for example, has a line of code with (length e $10). The variable e is not in either of the datasets ( m and y) being merged. So is this just a way of creating a new variable? And how does one decide on the length($) of the new variable.
Appearing before merge, set, update or modify statements this creates a variable in the first column(s) of length 10. The length setting would be determined by the user (you) for your purpose. The example code you show is one way to address a common issue of combining multiple data sets which do have a common variable with different lengths to set the length that you actually want. Typically in this case you set the length to the largest length of the common variable or possibly larger than either if you intend to add characters to it. In which case the length should be at least large enough to hold the longest expected value.
And then for this code below, what is its purpose?
proc sql noprint; select count (*) into: number from final where &var=&num1; quit;
%put Count=&number;
The purpose of the Proc Sql code is to count the number records from the data set FINAL where the resolved values of the macro variables in the clause &var=&num1 is true. Without knowing 1) the macro variable values or 2) the data set this could be checking a couple of conditions either a specific variable in the data set is equal to another variable, a variable is equal to a specific numeric value (most likely from the variable names) or two macro variable held values are equal (pretty stupid as you get all or 0 records).
The %put displays the number of identified records in the log.
@kmardinian wrote:
Hi, I am sure these are fairly easy question. But I am working with some code and there are couple parts I cannot seem to understand.
This data step for example, has a line of code with (length e $10). The variable e is not in either of the datasets ( m and y) being merged. So is this just a way of creating a new variable? And how does one decide on the length($) of the new variable.
Appearing before merge, set, update or modify statements this creates a variable in the first column(s) of length 10. The length setting would be determined by the user (you) for your purpose. The example code you show is one way to address a common issue of combining multiple data sets which do have a common variable with different lengths to set the length that you actually want. Typically in this case you set the length to the largest length of the common variable or possibly larger than either if you intend to add characters to it. In which case the length should be at least large enough to hold the longest expected value.
And then for this code below, what is its purpose?
proc sql noprint; select count (*) into: number from final where &var=&num1; quit;
%put Count=&number;
The purpose of the Proc Sql code is to count the number records from the data set FINAL where the resolved values of the macro variables in the clause &var=&num1 is true. Without knowing 1) the macro variable values or 2) the data set this could be checking a couple of conditions either a specific variable in the data set is equal to another variable, a variable is equal to a specific numeric value (most likely from the variable names) or two macro variable held values are equal (pretty stupid as you get all or 0 records).
The %put displays the number of identified records in the log.
Thank you so much! That clears it up very well for me!
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.