Wednesday, November 12, 2008

2 logics (Where also and Subsetting IF)

1. if City =: 'B';

It selects the observation if the data values of CITY variable starts with 'B' character (i.e,'B' at the first position in the City variable value).
Egg: City= "Bangalore", "Bahrain" etc., but not "bombay"

2. Where also Salary > 10000;

In general we cant use 2 where statements in a single data step. Means, if you executed a datastep which contains 2 where statements, then only the 2nd where statement will work. In this case you need to use where also statement instead of 2nd where statement.

Before:
data b;
set a;
where gender='M';
where salary > 10000;
run;

After:
data b;
set a;
where gender='M';
where also salary > 10000;
run;

Saturday, November 8, 2008

SORT Var Names in a Dataset

This code is used to sort variable names in a dataset. Means placing variables in ascending order in a dataset according to their names.

For more information, see the comments in code.

Here just you need to provide the library name and dataset name in which you want to see the variable names in their ascending order.

SAS Code:

/* options to print resolved macro code in log file */
options mlogic mprint symbolgen;

/* Macro code to sort variable names in a dataset */
%macro var_sort(library=,dataset=);

proc sql noprint;
select strip(name) into :vars separated by ' '
from dictionary.columns
where libname=upcase("&library") and memname=upcase("&dataset")
order by 1;
quit;

data &library..&dataset;
retain &vars;
set &library..&dataset;
run;

%mend var_sort;
/* end of the macro code */

/* Here just you need to provide the library name and dataset name in which you want to see the variable
names in their ascending order */
%var_sort(library=work,dataset=sample);


/*###########################################################################*/

/* Sample data to test this macro code */

data work.sample;
xy=1;
zx=2;
dc=3;
ab=4;
aa=1;
a=2;
cd='A';
run;

/* end of the sample data */

DateTime_Manip : a small SAS utility Tool.

This code is used to generate date and datetime values between a given range with a constant gap.

Here the constant gap may be day,week,month,year etc.,
For more details, see the comments in code.

Here you need to provide DATASET name in which you want to store created dates.
DT_SLOT: type of interval ex: DAY,WEEK,TENDAY,SEMIMONTH etc.,
START_VALUE: sas date, time, or datetime value that identifies a starting point.
END_VALUE: sas date, time, or datetime value that identifies a end point.
GAP: This is the number of intervals to shift the value of START_VALUE.
FORMA: Format which is used to display the value, easy to read.

SAS Code:


/* options to print resolved macro code in log file */
options mlogic mprint symbolgen;

/* use TIME8. format for time, DATE9. format for date and NLDATM21. format for datetime values */
/* use "01:00:00"t like values for Time, "1jan2001"d like values for date and
"1jan2001:23:59:59"dt like values for Datetime */
/* You can increment SECOND,MINUTE,HOUR(as 1st argument) for both Time and Datetime values,
DAY,WEEK,TENDAY,SEMIMONTH,MONTH,QTR,SEMIYEAR,YEAR etc., for Date values.
Just add DT for first argument, if you want to operate Date related things on Datetime values */

/* Macro code to generate date and datetime values with in the specified range, with specific gap. */
%macro dt_generate(dataset=,dt_slot=,start_value=,end_value=,gap=,forma=);

data &dataset;
dt_var=&start_value;
do while (dt_var<&end_value);
dt_var=intnx("&dt_slot",dt_var,&gap);
format dt_var &forma.;
output;
end;
run;

%mend dt_generate;
/* end of the macro code */

/* Here you need to provide DATASET name in which you want to store created dates/times.
DT_SLOT: type of interval ex: DAY,WEEK,TENDAY,SEMIMONTH etc.,
START_VALUE: sas date, time, or datetime value that identifies a starting point.
END_VALUE: sas date, time, or datetime value that identifies a end point.
GAP: This is the number of intervals to shift the value of START_VALUE.
FORMA: Format which is used to display the value, easy to read.
*/

%dt_generate(dataset=xxx,dt_slot=dtmonth,start_value="1jan2001:00:00:00"dt,end_value="1jan2002:00:00:00"dt,gap=1,forma=NLDATM21.);

--------------- END -----------------------------------------------------------

CheckVar - Existence of a variable in a dataset

This code is used to check for the existence of a variable in a dataset.
Just you need to provide library,dataset and variable name which you want to check.
For more details, see the comments in code.
Here just you need to provide the library.dataset name and the variable name which you want to search.

SAS Code:

/* Printing variable position in a dataset (>=1) and Existence of a variable (=0) */
%let dsid=%sysfunc(open(data1));
%let num=%sysfunc(varnum(&dsid,var1));
%let rc=%sysfunc(close(&dsid));
%put Variable is located in column:&num..;

------------------- END of the Code ----------------------------------------------

Thursday, May 8, 2008

About this Blog......

Hi,
I am Pavan Kumar Jupelli, working as a SAS Consultant with SAS R&D India Pvt. Ltd.,Pune.
My main intension of creating this blog is that, to share my knowledge on both Base SAS and SAS-Bussiness Intelligence which is most Powerful Package for Data Management,Analysis and Reporting. I want to share my views regarding SAS with you and at the same time looking for your valuable Suggestions.

My main intension is that, all the persons who want to know about SAS and who are already working on SAS can learn more interested things about SAS.
Hope this blog will help for your Career and Knowledge.
I am commited to put my total efforts on this Blog!!!!!
Cheers!