Hello Everyone,
I have a variable cars which has 4 different values. Some records have multiple values in it(the dataset looks like one row by showroom). I want to create a dataset with multiple records where I want to search for the a particular string value and give it one row each(dataset looks like it has multiple records by showroom).
I tried using Index, but didn't give the desired result.
Thanks
Here's a similar question, the answer is essentially the same except you have a different delimiter.
A more complex one:
And one more:
data PD3;
set PD2;
if cars ne "" and index(compress(lowcase(cars)),"audionroad") then flag1 = "Audi on road";
if cars ne "" and index(compress(lowcase(cars)),"benzonroad")then flag2 = "Benz on road";
if cars ne "" and index(compress(lowcase(cars)),"hondaonroad") then flag3 = "Honda on road";
if cars ne "" and index(compress(lowcase(cars)),"fordonroad") then flag4 = "Ford on road";
if cars ne "" and index(compress(lowcase(cars)),"porche") then flag5 = "Porche";
run;
data PD4;
group = 'A';
do until(done1);
set PD3(rename=(flag1=flag)) end=done1;
output;
end;
group = 'B';
do until(done2);
set PD3(rename=(flag2 = flag)) end=done2;
output;
end;
run;
It is not giving what I want.
If the screenshot shows what you want, then you need only one flag variable and use the output statement in each if.
if cars ne "" and index(compress(lowcase(cars)),"audionroad") then do;
flag = "Audi on road";
output;
end;
Have you read the links posted by @Reeza ? The first one suggest a solution, so have a look at the documentation of the function countw and scan and the output statement, all combined with a loop solves your problem.
Have you read the links posted by @Reeza ? The first one suggest a solution, so have a look at the documentation of the function countw and scan and the output statement. Used within a loop, your task can be solved in a single step.
Looks like your multiple cars are nicely separated by commas. So this may work:
data want;
set have;
do _N_=1 by 1;
car=scan(cars,_N_,',');
if car=' ' then leave;
output;
end;
drop cars;
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: