BookmarkSubscribeRSS Feed
Sami1234
Fluorite | Level 6

Dear Forum,

 

Would you please help me with the following.

Some of the variables in the dataset have decode variables and some of them don't.

I would like to separate them and if decode is not present then create a decode using the format associated with the variable.

In the code below, the decode is not available but if statement is not working.

 

 

%let code= status;

%let decode=statusd;

macro check;

data _null_;

%let dsid=%sysfunc(open(test));

%let countnum=%sysfunc(varnum(&dsid,&decode));

%let rc=%sysfunc(close(&dsid));

%put &countnum.;

 

%if &countnum. = 0 %then %do;

 

 

 data alltest;

 set test;

 keep var1 var2 code test;

 test=&decode ;

 run;

 

 

 *Replace the above dataset with your actual dataset and run the below step;

 

 data alltest1;

 set sashelp.vcolumn(keep=libname memname name type label format where=(libname='WORK' and memname='ALLTEST')) end=last;

 if _n_ eq 1 then call execute('proc sql; create table want as select ');

 if type='char' then call execute(strip(name)||' label="'||strip(label)||'"');

   else call execute('strip(put('||strip(name)||','||coalescec(strip(format),'best32.')||')) as '||strip(name)||' label="'||strip(label)||'"');

 if not last then call execute(',');

   else call execute(' from alltest;quit;');

 run;

 

proc sort data=want; by  by var1 var2;

run;

 

 

proc freq data= want noprint;

  by var1 var2;

 table decode* var11/  out=test5;

run;

 

 

%end;

 

%else %do;

 

proc freq data= test noprint;

 by var1 var2;

 table &var1.*&var1dc./ out=test5;

run;

 

 

%end;

 

%mend check;

 

%check;

 

 

Thank you!

6 REPLIES 6
Patrick
Opal | Level 21

@Sami1234 

"*Replace the above dataset with your actual dataset and run the below step;"

What is that? Homework, Interview assignment?

 

You're just throwing code at us, don't give us much explanation, no data and demonstrate no own effort. You need to help us to help you and you need to show some own effort unless you're willing to pay someone for consulting services.

Sami1234
Fluorite | Level 6

Hi, 

This code is working fine apart from this 'if statement' as this should be working if decode variable is not available.

wondering if there is any other way to check if the variable is not available then run the code below the if statement?

 

%if &countnum. = 0 %then %do;

Satish_Parida
Lapis Lazuli | Level 10
Please provide all the input datastes to the program. And the program is syntactically incorrect in so many places.
andreas_lds
Jade | Level 19

You already have a %put-statement writing the value of countnum to the log, so what's the value of the variable?

 

Also note, that you don't need a data-step to use %let-statements.

Tom
Super User Tom
Super User

You are missing the % in your %MACRO statement.

Sami1234
Fluorite | Level 6

% is already present in the code.

But i have sorted it now by adding the variable condition as below.

 

data _null_;

%let dsid=%sysfunc(open(test));

%let countnum=%sysfunc(varnum(&dsid,&decode));

%let rc=%sysfunc(close(&dsid));

 

 

data test1;

set test;

if decode=. then call symput('calculate' , 1);

run;

 

%if &calculate. =1 %then %do;

 

 

 data alltest;

 set test1;

 keep var1 var2 code test;

 test=&decode ;

 run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 739 views
  • 0 likes
  • 5 in conversation