SAS MERGE
SAS data merge problem
I have encountered a case where I am 100% sure the syntax is correct for merging datasets, but the result includes rows that are separate (while you expect them to see on the same row). This is likely because string variables have different length of trailing blanks. I recommend using STRIP function on IDs before the merge:
data lengthn;
input string $char8.;
kaz = strip(string);
datalines;
abcd
abcd
abcd
abcdefgh
x y z
;
proc print data=lengthn;
run;
This situation can occur easily, but it does not generate error messages. This is a serious problem.
ORIGINAL REFERENCE:
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002295689.htm
How to copy names of files from a window's folder
Imagine you have 100 excel files in your windows folder (or even 100,000) and you need to write names of those files in your SAS syntax (or any statistical software programs). Typing 100 names is time consuming. Instead you can use a cmd prompt at Windows and copy file names into a text file.
On Windows (7 in my case)
START --> RUN ..
Type in "cmd" in the pop-up window and OK it.
You get a small black window. Type "cd" at the prompt to get to the folder you want to go.
Examples:
- cd Music (You will go to a folder Music;but this folder has to exist in the folder you are currently in)
- cd C:\temp (This will directly let you go to the folder you want to go regardless of where you are currently in the holder structure; I used an example of C:\temp)
- cd .. (You go up one folder structure)
Once you get to the folder, you will do this (this is an example of getting text files that have extention "txt" e.g., abc.txt).
dir *.txt > example_of_cmd_text.txt
This will copy all files that end with extention ".txt" into a text file. The resulting text file will include all file names in the folder. This is an example:
http://www.nippondream.com/file/example_of_cmd_text.txt
It comes with additional pieces of information you may not need (e.g., date), so you want to open this with Excel and get exact information you need.
I googled for CMD commands tutorials:
SAS DATASETS to delete all files in work directory
proc datasets library = work kill nolist;
quit;
How to delete datasets in SAS using PROC DATASETS
Deleting all temporary datasets:
proc datasets library = work kill nolist;
quit;
Deleting specific datasets in the temp directory:
proc datasets;
delete kaz1 estes diminfo concon FITSTAT hlm1;
run;
How to check if a sas dataset was created or not
/*CHECK IF COVPARMS is CREATED*//*CHECK IF COVPARMS is CREATED*/
%macro checkds(dsn);
%if %sysfunc(exist(&dsn)) %then %do;
proc print data = &dsn;run;
data HLM_OR_NOT;value="YES_HLM";run;
%end;
%else %do;
data covparms;CovParm="NoCov";run;
data HLM_OR_NOT;value="NO_HLM ";run;
%end;
%mend checkds;
/* Invoke the macro, pass a non-existent data set name to test */
%checkds(work.covparms);
data _null_;set hlm_or_not;
call symput ("hlm_or_not",value);
run;
%put &hlm_or_not;
/*CHECK IF COVPARMS is CREATED*//*CHECK IF COVPARMS is CREATED*/
Numeric <--> String Conversion
/* convert numeric to character */ char_year=put(year,4.0); /* convert character to numeric */ char1='1234.56'; num1=input(char,7.);
Keep variables when they have the same prefix
I want to keep variables when they start from SUM_*. Could you advise?
You can use the colon modifier to do this. I have include sample code below.
data sums;
set these2;
keep subgroup
sum_:;
run;
Creating a sequence variable by subgroup
data indiv4;
set indiv3;
if N_entry > 9;
run;
proc sort;by subgroup;
run;
data indiv5;
retain sequence;
set indiv4;
by subgroup;
if first.subgroup then sequence=0;
sequence=sequence+1;
run;