Hi, I have been given a few string values and no variables (fields) names for these values. Is it possible to search for these values in a dataset, which is located in a unix environment, and to find the variables (fields) these values belongs to?
Anytime.
Yes.
Where are the "string values" stored to find?
You might need to define what sort of match is involved. Consider if one of your strings to find is "apple" do you want to report apple as found in "applesauce"? Is case going to important such as find "apple" but not "Apple".
What should the result look like?
Here is one example of some ways to search and report what is found. There are other ways to find things in strings but you need to be more explicit. This uses a data set you should have available to run the code with.
data example; set sashelp.class indsname=dsn; dataset=dsn; /* create something that will hold all the character values*/ array c (*) _character_; /* one way to hold a few strings to search for*/ array t (3) $ 10 _temporary_ ('John','M','Xyzzy'); do i=1 to dim(c); do j=1 to dim(t); /* search for exact match*/ if t[j] = c[i] then do; exactvar = vname(c[i]); foundexact=t[j]; obs=_n_; end; if (find(c[i],t[j],'it')>0) and ( t[j] ne c[i] )then do; partialvar = vname(c[i]); foundpartial=t[j]; obs=_n_; end; if not missing(coalescec(exactvar,partialvar)) then do; output; call missing(exactvar, partialvar,foundexact,foundpartial); end; end; end; keep dataset exactvar foundexact obs partialvar foundpartial; label dataset = 'Data set searched' exactvar= 'Name of variable with equal match' foundexact= 'Matched value' obs = 'Observation number with match' partialvar = 'Name of variable with partial match' foundpartial='Partial matched value' ; run; /* search for appear in string*/
Key elements the _character_ in the array statement places all the character variables into an array which we can loop over. The temporary array is one way to place a small number of values into a handy construct to search. My example provides one that I know is not in the data just to demonstrate that.
There are two different comparisons, exact and partial substring ignoring case. Note that any "exact" match will likely be a "partial" match so I tried to avoid duplicating the output.
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.