I'm trying to organize a dataset in a specific way with a list of variables that changes. The issue I'm having is that I don't always know the actual number of variables I'm going to have in my dataset. I've done this previously with either a PROC SQL
statement or a RETAIN
statement after the data statement where the list of variables was static.
My data looks like this:
APPNUM DATE REASON1 REASON2 REASON3 REASON4 NAME1 NAME2 NAME3 NAME4
123 1/1/2017 X Y Z A Jon Mary Tom Suzie
I want it to look like this:
APPNUM DATE REASON1 NAME1 REASON2 NAME2 etc
123 1/1/2017 X Jon Y Mary etc
This would be easy with sql or a retain statement. However, I am using loops, etc to pull these variables together, and the number of variables presented is dependent upon my input data. Some days there may be 20 instances of REASON/NAME and others there may be 1 of each.
I tried the below code to pull a list of variable names, then order the APPNUM, DATE, then finally order by the LAST digit of the variable name. I.E. 1,1,2,2,3,3 - but I was unsuccessful. The list was being stored properly - no errors, but when resolving the value of &VARLIST. they are not ordered as expected. Has anyone ever tried and accomplished this?
PROC SQL;
SELECT NAME INTO :VARLIST SEPARATED BY ','
FROM DICTIONARY.COLUMNS
WHERE LIBNAME = 'WORK'
AND MEMNAME = 'SFINAL'
ORDER BY NAME, SUBSTR(NAME,LENGTH(NAME)-1);
QUIT;
The above code would order something like this:
APPNUM, DATE, NAME1...2...3..., REASON1...2...3...
and not:
APPNUM, DATE, NAME1, REASON1, NAME2, REASON2....