Posts

Showing posts with the label Do-loop

Renaming All Variables in a SAS Data Set Using the SASHELP VIEWS

*Create a temporary dataset... DSN; data dsn; a=1; b=2; c=3; d=4; e=5; f=6; run; % macro test(lib,dsn); */1)*/ data _null_; set sashelp.vtable(where=(libname=" &LIB " and memname= "&DSN" )); call symput( 'nvars', nvar); run; */2)*/ data dsn; set sashelp.vcolumn(where=(libname ="&LIB" and memname= "&DSN" )); call symput(cats( "var" ,_n_),name); run; */3)*/ proc datasets library =&LIB; modify &DSN; rename % do i = 1 % to &nvars; &&var&i=Rename_&&var&i. % end ; ; quit; run; % mend ; %test (WORK,DSN); After submitting the above program... the output looks like this.... Output: Rename_a Rename_b Rename_c Rename_d Rename_e Rename_f 1 2 3 4 5 6 Here is a way I know of.. to rename all the variables in the dataset; It can be done using the SASHELP views as follows: 1) The 1st step of the program determines the total number of variables inside the dataset with the help of SA...

How to merge data sets with a common variable?

Here is the simple way of merging the data sets with a common variable if the datasets has the same prefix name. For example: col1-col10, dsn1-dsn 7 , or data1 to data10 with common variable of ID. Considering we have 10 datsets and all of them having the same prefix data; %macro mymerge (n); data merged; merge %do i = 1 % to &n; data&i %end; ; /* this additional ';' is necessary, the first ';' is for the "%end", while the second ';' is for "Merge"*/; by id; run; %mend; %mymerge(10)