BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rohithverma
Obsidian | Level 7

I have a dataset 1 as mentioned below

sno name
1     ganesh
2     akhila
3     aditya
4     ashok
5     SREE
6      yash

 

I have dataset2 as mentioned below

sno  gender    age

1     male        23
3     male       26
4    female     28
5     male        29

 

So, where the sno matches the values from the second table must be copied to the varaibles newvar1 and newvar2 and if not " NA" should appear as mentioned below .

sno  name  newvar1  newvar2
1      ganesh male 23
2       akhila NA NA
3       aditya male 26
4       ashok female 28
5       SREE FEMALE 28
6         yash NA NA

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @rohithverma 

 

Are you sure you want to change the type of the age variable?

Is it for reporting purpose?

 

data dataset1;
	input sno name $;
	datalines;
1 ganesh
2 akhila
3 aditya
4 ashok 
5 SREE
6 yash
;
run;

data dataset2;
	input sno gender $ age;
	datalines;
1 male 23
3 male 26
4 female 28
5 male 29
;
run;

data want (drop=agen);
	merge dataset1 (in=x) dataset2 (in=y rename=(age = agen));
	by SNO;
	if x=1;
	age = put(agen,best.);
	if y=0 then do;
		age = "NA";
		gender = "NA";
	end;
run;

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

In a DATA step, you merge the two data sets, using BY SNO;

--
Paige Miller
rohithverma
Obsidian | Level 7
I have done with merge but in need to compare this for three datasets .So i need a single set of program like using UPDATE and SET
Kurt_Bremser
Super User

@rohithverma wrote:
I have done with merge but in need to compare this for three datasets .So i need a single set of program like using UPDATE and SET

Your initial posts mentions only TWO datasets. Make up your mind.

What you described in the OP is a MERGE, period.

PaigeMiller
Diamond | Level 26

@rohithverma wrote:
I have done with merge but in need to compare this for three datasets .So i need a single set of program like using UPDATE and SET

MERGE is the perfect command here, not UPDATE or SET.

 

Do you really have to have NA? MERGE will create missing values, that ought to be sufficient for any reasonable purpose.

--
Paige Miller
Kurt_Bremser
Super User

Run a DATA step MERGE with the in= dataset option for dataset2, and by sno;

If the in= variable for dataset2 is false, set gender to NA, and let age stay missing (as it is numeric, it can't have a character value). If you really want NA for missing, create a custom format for that.

ed_sas_member
Meteorite | Level 14

Hi @rohithverma 

 

Are you sure you want to change the type of the age variable?

Is it for reporting purpose?

 

data dataset1;
	input sno name $;
	datalines;
1 ganesh
2 akhila
3 aditya
4 ashok 
5 SREE
6 yash
;
run;

data dataset2;
	input sno gender $ age;
	datalines;
1 male 23
3 male 26
4 female 28
5 male 29
;
run;

data want (drop=agen);
	merge dataset1 (in=x) dataset2 (in=y rename=(age = agen));
	by SNO;
	if x=1;
	age = put(agen,best.);
	if y=0 then do;
		age = "NA";
		gender = "NA";
	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
  • 6 replies
  • 894 views
  • 1 like
  • 4 in conversation