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

Hi, I have the scenario that I have to use retain statement for claimID variable. When some data have ClaimID as numeric value and some have as character value, can I create a macro that passes through with checking the ClaimID type?

I need to create something like %if CLAIMID = numeric value %then %do;

 

data work.createclaimID;

 

%if CLAIMID = numeric value %then %do;

   length INI_ClaimID 8. ;

   set sortedsrc;

    by MemID

 

   retain INI_;

 

 

   if first.MemID then do;

       INI_ClaimID   = .;

   end;

%end;

 

 

%if CLAIMID = character value %then %do;

   length INI_ClaimID $60. ;

   set sortedsrc;

    by MemID

 

   retain INI_;

 

 

   if first.MemID then do;

       INI_ClaimID   = ‘ ‘;

   end;

%end;

1 ACCEPTED SOLUTION

Accepted Solutions
JenniferK
Calcite | Level 5

Here is the solution if anybody needs it.


/** sample data set **/
data sortedsrc;
claimid=123;
memid=1;
run;

data sortedsrc2;
claimid='123';
memid=1;
run;

%macro lst(dsn);
/** open dataset **/
%let dsid=%sysfunc(open(&dsn));

/** retrieve the type of the variable claimid and place in macro variable &typ **/
%let x=%sysfunc(varnum(&dsid,claimid));
%let typ=%sysfunc(vartype(&dsid,&x));

/** close dataset **/
%let rc=%sysfunc(close(&dsid));

data final_&dsn;
set &dsn;
by MemID;
retain INI_:;

%if &typ = C %then %do;
length INI_ClaimID $60. ;
if first.MemID then do;
INI_ClaimID = ' ';
end;
%end;
%else %do;
length INI_ClaimID 8. ;
if first.MemID then do;
INI_ClaimID = .;
end;
%end;
run;

%mend lst;
%lst(sortedsrc);
%lst(sortedsrc2);

View solution in original post

2 REPLIES 2
JenniferK
Calcite | Level 5

Here is the solution if anybody needs it.


/** sample data set **/
data sortedsrc;
claimid=123;
memid=1;
run;

data sortedsrc2;
claimid='123';
memid=1;
run;

%macro lst(dsn);
/** open dataset **/
%let dsid=%sysfunc(open(&dsn));

/** retrieve the type of the variable claimid and place in macro variable &typ **/
%let x=%sysfunc(varnum(&dsid,claimid));
%let typ=%sysfunc(vartype(&dsid,&x));

/** close dataset **/
%let rc=%sysfunc(close(&dsid));

data final_&dsn;
set &dsn;
by MemID;
retain INI_:;

%if &typ = C %then %do;
length INI_ClaimID $60. ;
if first.MemID then do;
INI_ClaimID = ' ';
end;
%end;
%else %do;
length INI_ClaimID 8. ;
if first.MemID then do;
INI_ClaimID = .;
end;
%end;
run;

%mend lst;
%lst(sortedsrc);
%lst(sortedsrc2);

Reeza
Super User

Congrats on finding the answer yourself 🙂

 

One suggestion for your code, to make it more general, you can use CALL MISSING which applies to both numeric/character variables instead of assigning missing values. Length statements usually need to be before the SET statement for variables that already exist as well but if it worked for you I guess you're fine. 

 

data final_&dsn;

%if &typ = C %then length INI_ClaimID $60. ; %else length INI_ClaimID 8. ;

set &dsn; by MemID; retain INI_:; if first.MemID then call missing(INI_ClaimID); run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 727 views
  • 2 likes
  • 2 in conversation