data t; set FDAT; period="A"; if strip(EXT) = "pine" then do; EXD = 5; output; EXD = 10; output; EXD = 15; output; end; if strip(EXT) = "Rdone" then do; EXD = 2; output; EXD = 4; output; EXD = 6; output; end; run;
if my variable EXT has pine in it then i want to assign new variable exd and randomly assign values 5,10,15 for all id's and the same with the next step as mentioned in the code.
Somehow it doesnt work and creates more records than existing dataset. I am not sure if the logic i am using is correct.
Any help?
2 things.
1) Since you use explicit output statements, you are not guaranteed to get the same number of rows in the two data sets. It will depend on the EXT variable.
2) Since you want to randomly assign values to the EXD variable if "EXT has pine in it", then your if statement is not sufficient.
There is not much to go on, but I think this is closer to what you want
data t;
set FDAT;
period="A";
array valuespine [3] _temporary_ (5 10 15);
array valuesRdone [3] _temporary_ (2 4 6);
if findw(EXT,'pine', 'i')>0 then do;
EXD=valuespine[rand("integer", 1, 3)];
end;
if findw(EXT,'Rdone', 'i')>0 then do;
EXD=valuesRdone[rand("integer", 1, 3)];
end;
run;
somehow it does not resolve if i use condition if period="A" then do;
i get empty values.
attached test data.
data t;
set FDAT;
period="A";
if strip(EXT) = "pine" then
do I = 5 to 15 by 5;
EXD = i;
output;
end;
if strip(EXT) = "Rdone" then
do I = 2 to 6 by 2;
EXD = i;
output;
end;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.