BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Rakesh93
Calcite | Level 5
How to copy data of one variable to another in SAS.
I was using this program
Data DM6;
Set DM5;
RFS TFTC=RFXSTDTC;
Run;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@Rakesh93 wrote:


RFS TFTC=RFXSTDTC;

You have an unnecessary (and wrong) space in the code. You want RFSTFTC without a space:

data DM6;
   set DM5;
   RFSTFTC=RFXSTDTC;
run;

Editor's Note:

Special thanks to @ChrisNZ and @hashman for sharing how to conditionally assign a value if the original variable is missing using the COALESCE function for numeric values, and the COALESCEC function for character values:

data _NULL_;
   Num_1=.;
   Var_1='    ';
   Var_2='    ';
	NUM_A=10;
	VAR_A='Test';
	put 'NOTE: Before ***********************';
	put 'NOTE: ' NUM_1= NUM_2= NUM_A=;
	put 'NOTE- ' VAR_1= VAR_2= VAR_A=;
	NUM_1=coalesce(NUM_1, NUM_A);
	VAR_1=coalescec(VAR_1, VAR_A);
	put 'NOTE: After first *******************';
	put 'NOTE: ' NUM_1= NUM_2= NUM_A=;
	put 'NOTE- ' VAR_1= VAR_2= VAR_A=;
	NUM_2=coalesce(NUM_1, NUM_A);
	VAR_2=coalescec(VAR_1, VAR_A);
	put 'NOTE: After last *******************';
	put 'NOTE: ' NUM_1= NUM_2= NUM_A=;
	put 'NOTE- ' VAR_1= VAR_2= VAR_A=;
run;

And the result:

NOTE: Before ***********************
NOTE: Num_1=. NUM_2=. NUM_A=10
      Var_1=  Var_2=  VAR_A=Test

NOTE: After first *******************
NOTE: Num_1=10 NUM_2=. NUM_A=10
      Var_1=Test Var_2=  VAR_A=Test

NOTE: After last *******************
NOTE: Num_1=10 NUM_2=10 NUM_A=10
      Var_1=Test Var_2=Test VAR_A=Test

 

 

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

@Rakesh93 wrote:


RFS TFTC=RFXSTDTC;

You have an unnecessary (and wrong) space in the code. You want RFSTFTC without a space:

data DM6;
   set DM5;
   RFSTFTC=RFXSTDTC;
run;

Editor's Note:

Special thanks to @ChrisNZ and @hashman for sharing how to conditionally assign a value if the original variable is missing using the COALESCE function for numeric values, and the COALESCEC function for character values:

data _NULL_;
   Num_1=.;
   Var_1='    ';
   Var_2='    ';
	NUM_A=10;
	VAR_A='Test';
	put 'NOTE: Before ***********************';
	put 'NOTE: ' NUM_1= NUM_2= NUM_A=;
	put 'NOTE- ' VAR_1= VAR_2= VAR_A=;
	NUM_1=coalesce(NUM_1, NUM_A);
	VAR_1=coalescec(VAR_1, VAR_A);
	put 'NOTE: After first *******************';
	put 'NOTE: ' NUM_1= NUM_2= NUM_A=;
	put 'NOTE- ' VAR_1= VAR_2= VAR_A=;
	NUM_2=coalesce(NUM_1, NUM_A);
	VAR_2=coalescec(VAR_1, VAR_A);
	put 'NOTE: After last *******************';
	put 'NOTE: ' NUM_1= NUM_2= NUM_A=;
	put 'NOTE- ' VAR_1= VAR_2= VAR_A=;
run;

And the result:

NOTE: Before ***********************
NOTE: Num_1=. NUM_2=. NUM_A=10
      Var_1=  Var_2=  VAR_A=Test

NOTE: After first *******************
NOTE: Num_1=10 NUM_2=. NUM_A=10
      Var_1=Test Var_2=  VAR_A=Test

NOTE: After last *******************
NOTE: Num_1=10 NUM_2=10 NUM_A=10
      Var_1=Test Var_2=Test VAR_A=Test

 

 

--
Paige Miller
ravig
Obsidian | Level 7

Lets say there are two variables VAR1 and VAR2. VAR2 have some missing values and you want to fill the missing values with values from VAR1.

 

Data NEW;

set Sorucedataset;

If Var2=" " then  VAR2=VAR1;else VAR2=VAR2;

run;

hashman
Ammonite | Level 13

@ravig: Heed what @ChrisNZ has said.

If your variables are character, use the COALESCEC function instead.   

If you want to make you code data type insensitive, use SQL where the single COALESCE function is good for both numeric or character variables, e.g.:

proc sql ;
  create table want as select coalesce (var1, var2) as var2 from have ;
quit ;

Kind regards

Paul D.

ChrisNZ
Tourmaline | Level 20

Like this?

RFSTFTC=coalesce(RFSTFTC,RFXSTDTC);

for a numeric variable.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 4 replies
  • 5323 views
  • 2 likes
  • 5 in conversation