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;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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