BookmarkSubscribeRSS Feed
vraj1
Quartz | Level 8
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?

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

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;
vraj1
Quartz | Level 8

somehow it does not resolve if i use condition if period="A" then do;

i get empty values.

attached test data.

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

 

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2511 views
  • 0 likes
  • 3 in conversation