Syntax for Sequential Variable Names

Viewed 32

I am struggling to compile my large dataset and am assuming syntax commands are the answer, however, I am not skilled at all with syntax. My questions are specific to what syntax commands (or other methods) I should use to create hundreds/thousands of new variable names so I do not need to do it manually.

I am working with a dataset involving intimate partner homicides and domestic violence services utilization among victims from 2012-2021 (10 years), examined monthly (120 months). Across that timeframe, I have a three variable name set (REC [number of clients who received services], CALL [number of calls for services], HOUR [number of hours advocates/employees spent providing services]) that needs to be repeated monthly Jan-Dec across 10 years 2012-2021 and 39 separate services. See below:

MonthYear_REC_ServiceName MonthYear_CALL_ServiceName MonthYear_HOUR_ServiceName

"Month" in the above is Jan-Dec (01-12), "Year" is 2012-2021 (12-21), and "ServiceName" would be replaced with 39 different services. As an example for the year 2017 and "Shelter" services:

0117_REC_Shelter 0117_CALL_Shelter 0117_HOUR_Shelter 0217_REC_Shelter 0217_CALL_Shelter 0217_HOUR_Shelter 0317_REC_Shelter 0317_CALL_Shelter 0317_HOUR_Shelter .....so on and so forth until December of 2017.

To further explain: This sequential monthly order would need to be repeated for each year in the 2012-2021 timeframe for each of 39 services for which I have data. "Shelter" services are shown as an example above, but I also need the same set of variable names across 38 other service types such as group counseling, legal advocacy, economic assistance, etc.

My overall question is (sorry for the repetition)- What syntax commands would I need to input to create this MASSIVE amount of variable names/variables? I hope this makes sense to everyone in the same way it makes sense to me! Sorry for the length and thank you in advance.

Best, Shannon H.

1 Answers

Assuming what you want is to create an empty dataset with all the variable names you described, this will do it:

INPUT PROGRAM.
    LOOP ind = 1 to (12*10*3*39).
        END CASE.
    END LOOP.
    END FILE.
END INPUT PROGRAM.
EXECUTE.

do repeat vr=month year set service/vl=12 10 3 39.
    compute vr=mod(ind,vl).
    recode vr (0=vl).
    compute ind=trunc((ind-1)/vl)+1.
end repeat.
compute year=year+11.
formats all (f2).
alter type month year (a2) set (a4).
compute month = char.lpad(ltrim(month), 2, "0").
recode set ("   1"="REC")("   2"="CALL")("   3"="HOUR").
* I suggest at this point you use "match files" to match the service numbers here with a list of service names.
* The following code creates fictitious service names instead to demonstrate how to use them.
string serviceName (a20) vrnm (a50).
compute serviceName=concat("service", char.lpad(ltrim(string(service, f2)), 2, "0") ).
* now to create the final variable names.
compute vrnm=concat(month, year, "_", set, "_", serviceNAme).
flip NEWNAMES  = vrnm.
select if CASE_LBL="".
exe.
Related