BookmarkSubscribeRSS Feed
Varun
Calcite | Level 5

I have this SAS sample code:

data BEFORE;

    input v1 v2;

datalines;

1 2

;

data AFTER;

    put 'Before IF: ' _ALL_;

    if _N_ = 1 then set BEFORE;

    put 'After  IF : ' _ALL_;

run;

The output is:

BEFORE: v1=. v2=. _ERROR_=0 _N_=1

AFTER : v1=1 v2=2 _ERROR_=0 _N_=1

BEFORE: v1=1 v2=2 _ERROR_=0 _N_=2

AFTER : v1=1 v2=2 _ERROR_=0 _N_=2

And the output file contains:

Obs    v1    v2

1      1     2

2      1     2

I would like to understand why two observations set in the new dataset with repeated values. Please clerify.

2 REPLIES 2
Hima
Obsidian | Level 7

The reason why it's appearing twice is because of the below code

if _N_ = 1 then set BEFORE; 

This step copies the results from before and pastes it After data set.

    put 'After  IF : ' _ALL_;

_ALL_ does the same but it copies the results from AFTER and pastes it again.

Fix to the code would be

data BEFORE;
input v1 v2;
datalines;
1 2
;


data AFTER;
put 'Before IF: ' _ALL_;
set BEFORE;
put 'After  IF : ' _ALL_;
;
run;

proc print; run;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 2 replies
  • 832 views
  • 0 likes
  • 3 in conversation