BookmarkSubscribeRSS Feed
wcp_fnfg
Obsidian | Level 7

Some test code:

data one;

input cat1 $ cat2 $;

datalines;

mortgage fixed

mortgage arm

;

run;

data two;

input group1 $ group2 $;

datalines;

card standard

card reward

;

run;

data three;

set one two;

if missing(cat2) then cat2 = group2;

x = coalesceC(cat2, group2);

run;

For Cards, why would cat2 and X end up as "standard" for both records??

mortgagefixedfixed
mortgagearmarm
standardcardstandardstandard
standardcardrewardstandard
2 REPLIES 2
Tom
Super User Tom
Super User

CAT2 is RETAINed because it is coming from one of the datasets on the SET statement.  All such variables are retained.  It only appears not to be retained because the SET statement fills it with new values (or when you reach the end missing values).

You do not normally notice is this type of program because you normally do not assign a value one of the variables from your INPUT dataset after you have read past the end.

Add a couple of PUT statements and you can see what is happening.

BEFORE: dsname=  cat1=  cat2=  group1=  group2=  x=  _ERROR_=0 _N_=1

AFTER: dsname=WORK.ONE cat1=mortgage cat2=fixed group1=  group2=  x=fixed _ERROR_=0 _N_=1

BEFORE: dsname=WORK.ONE cat1=mortgage cat2=fixed group1=  group2=  x=  _ERROR_=0 _N_=2

AFTER: dsname=WORK.ONE cat1=mortgage cat2=arm group1=  group2=  x=arm _ERROR_=0 _N_=2

BEFORE: dsname=WORK.ONE cat1=mortgage cat2=arm group1=  group2=  x=  _ERROR_=0 _N_=3

AFTER: dsname=WORK.TWO cat1=  cat2=standard group1=card group2=standard x=standard _ERROR_=0 _N_=3

BEFORE: dsname=WORK.TWO cat1=  cat2=standard group1=card group2=standard x=  _ERROR_=0 _N_=4

AFTER: dsname=WORK.TWO cat1=  cat2=standard group1=card group2=reward x=standard _ERROR_=0 _N_=4

BEFORE: dsname=WORK.TWO cat1=  cat2=standard group1=card group2=reward x=  _ERROR_=0 _N_=5

Astounding
PROC Star

Besides retaining SET statement variables, there's one more complicating factor, which PUT messages can't capture 100% because it's part of SET statement execution.

When, if ever, should the software reinitialize CAT2 to missing?  The answer is when reading the first observation from the data set TWO.  When reading the first observation from a data set, SET statement variables that do not appear in that data set get reset to missing.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1023 views
  • 1 like
  • 3 in conversation