SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shiv999
Calcite | Level 5
Hi , I have a dataset as shown below. I need to only pick the values like 2081 2082 2084 and 2084 into a 4 macro variable and ignore if 0000. Can you please suggest

Column1 | column2 | column3 | Column4
0000 | 0000 | 0000 | 2084
2081 | 0000 | 0000 | 0000
0000 | 2082 | 2083 | 0000
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile cards dlm='|';
input Column1- Column4;
cards;
0000 | 0000 | 0000 | 2084
2081 | 0000 | 0000 | 0000
0000 | 2082 | 2083 | 0000
;

proc sql;
select max(Column1),max(Column2),max(Column3),max(Column4)
  into : c1,: c2,: c3,: c4
   from have;
quit;

%put _user_ ;

View solution in original post

10 REPLIES 10
ChrisNZ
Tourmaline | Level 20

There's always 4 variables, 3 rows, and 4 values?

shiv999
Calcite | Level 5
Rows can be between 1 to 4 it depends
shiv999
Calcite | Level 5
The given dataset createf in a macro ,Based on the macro parameters.these dataset rows would be between 1 to 4 depend on the data. Now once I get the unique values I will pass the values into below code using a macro variable
Kurt_Bremser
Super User

And what will that below code do?

I ask because in 90% (if not 100%) of cases it is not necessary to create a list of macro variables, but instead the code can be created (or called) directly from the dataset.

shiv999
Calcite | Level 5
Yes, I understand but existing code already use the macro variable . So I doesn't want to change the approach . Is there a way we do in macro variable?
Kurt_Bremser
Super User
data _null_;
set have;
retain counter 0;
array col {*} column:;
do i = 1 to dim(col);
  if col{i} ne '0000'
  then do;
    counter +1;
    call symputx(cats('mvar',counter),col{i});
  end;
end;
run;

Mind that perpetuating a bad code structure by adding even more bad code to it is always a bad idea. Improving suboptimal code is a worthy investment into your own future.

shiv999
Calcite | Level 5
Thanks
Ksharp
Super User
data have;
infile cards dlm='|';
input Column1- Column4;
cards;
0000 | 0000 | 0000 | 2084
2081 | 0000 | 0000 | 0000
0000 | 2082 | 2083 | 0000
;

proc sql;
select max(Column1),max(Column2),max(Column3),max(Column4)
  into : c1,: c2,: c3,: c4
   from have;
quit;

%put _user_ ;
shiv999
Calcite | Level 5
Thanks it's very awesome

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
  • 10 replies
  • 2037 views
  • 1 like
  • 4 in conversation