Dear SAS community:
I have two columns. The first one has the geographic dispersion of a firm by states. The states are abbreviated by two letters and separated by a dash.
The second column has the states that were affected by some kind of disaster. I am interested in creating a dummy variable equal to 1 if the geogr_disper variable contains any of the affected_states. What will be a best option for this. I tried findw but it did not work.
data WORK.have;
infile datalines dsd truncover;
input geogr_disper:$200. affected_state:$200.;
datalines4;
WI-TX-OH-NH-MS-ME-KY-GA-CA-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
WA-TX-PA-IN-GA-CA-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
TX-PA-OK-OH-NY-LA-KS-IN-IL-AL-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
OR-NY-NV-CA-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
FL-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
WA-NY-IN-IL-HI-DE-CA-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
WA-VT-UT-TX-TN-PA-OK-OH-NY-NJ-NH-MO-MS-MN-MI-LA-KY-IL-FL-DE-CT-AR-AZ-AL-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
TX-NJ-NV-AZ-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
WA-NH-DE-CT-AZ-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
PA-IL-FL-DE-CA-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
PR-WI-WV-WA-VA-VT-UT-TX-RI-PA-OR-OH-NC-NY-NJ-NH-NV-MO-MI-MA-MD-ME-KS-IN-IL-ID-HI-FL-DE-CT-CO-CA-AZ-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
WY-WV-VA-TX-TN-PA-OH-NY-NM-NJ-NV-MS-MD-IN-IL-FL-DE-CT-CA-AZ-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
WI-SD-OK-NE-MO-KS-IL-CO-AZ-,DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV
WI-TN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('/', translate(AFFECTED_STATE,'|','-'), '/')
,trim(GEOGR_DISPER)) > 0 ;
GEOGR_DISPER | AFFECTED_STATE | AFFECTED |
---|---|---|
WI-TX-OH-NH-MS-ME-KY-GA-CA- | DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV | 0 |
WA-TX-PA-IN-GA-CA- | DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV | 1 |
TX-PA-OK-OH-NY-LA-KS-IN-IL-AL- | DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV | 1 |
OR-NY-NV-CA- | DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV | 1 |
FL- | DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV | 0 |
Try this
data want;
set have;
len_geogr=length(compress(geogr_disper,,'a'));
len_affct=length(compress(affected_state,,'a'));
format matched_geogr $200.;
do i=1 to len_geogr;
curr_geogr=scan(geogr_disper,i,'-','m');
do j=1 to len_affct;
curr_affct=scan(affected_state,j,'-','m');
if curr_geogr=curr_affct then do;
matched_geogr=catx(" ",matched_geogr,curr_geogr);
dummy=1;
end;
end;
end;
if dummy ne 1 then dummy=0;
drop len_geogr len_affct i j curr_affct curr_geogr;
run;
Could be done with regular expressions:
data WORK.have;
infile datalines dsd truncover;
input geogr_disper:$200. affected_state:$200.;
affected = prxMatch(
cats("/", translate(substr(geogr_disper,1,length(geogr_disper)-1),"|","-"), "/"),
affected_state) > 0;
datalines4;
...
Or:
AFFECTED = prxMatch(
cats('/', translate(AFFECTED_STATE,'|','-'), '/')
,trim(GEOGR_DISPER)) > 0 ;
GEOGR_DISPER | AFFECTED_STATE | AFFECTED |
---|---|---|
WI-TX-OH-NH-MS-ME-KY-GA-CA- | DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV | 0 |
WA-TX-PA-IN-GA-CA- | DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV | 1 |
TX-PA-OK-OH-NY-LA-KS-IN-IL-AL- | DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV | 1 |
OR-NY-NV-CA- | DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV | 1 |
FL- | DE-MD-NC-NJ-NY-PA-RI-VA-VT-WV | 0 |
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.