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
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;
In a DATA step, you merge the two data sets, using BY SNO;
@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.
@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.
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.
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;
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.