BookmarkSubscribeRSS Feed
hannah_hortlik
Obsidian | Level 7

Hey guys,
I have to write a syntax and give it to an institute where they will check it and let it run with the original database.
I just have a Test/Dummy data Base.
I want to get my results from every year from 2005 to 2017, it doesn't matter if every year is in a seperate file or if it's one big file/results with all years pooled.


The original Data is named "drg2005_sf", "drg2006_sf" for every year and is in the folder with all the years "sasuser.v94"
So here is the head of my syntax:

/*** compress files and output alignment ***/
options nocenter linesize=256 pagesize=90 compress=yes;

/*** macros for paths ***/
%let datenpfad = ~/sasuser.v94;
%let outputpfad = ~/sasuser.v94;

/*** Macros for file and output names ***/
%let datenname = drg;
%let outputname = drg_out;

/*** Libname assign ***/
libname daten "&datenpfad";

down below I pick the years I want to use:

%let startjahr = 2005;
%let endejahr = 2017;
%do year = &startjahr %to &endejahr;

data drg_&year;
set daten.&datenname.&year

 


But if I let it run like this, the Error says:
ERROR: Datei DATEN.DRG2005.DATA existiert nicht.
ERROR: Datei DATEN.DRG2006.DATA existiert nicht.

 


I don't know why it can't find my "drg2005_sf" data or any data of any year, where do I have to make a change?
I tried to change the "Datenname" but it can't find my data.
As soon as I delete the &year in the last row it works in any way, maybe for just one year? I don't know, but not the way I want.


Hope some one can help me, I'm absolute new in sas an statistics at all but I hope I could describe you my problem, if not please text me and I will try to explain it better.
have a nice day!

11 REPLIES 11
hannah_hortlik
Obsidian | Level 7

Oh my god thank you so much.

Can I ask you another question?

 

Down below in my syntax I have:

 

proc freq data = drg;
title "Output Nr. 1a: Blablabla &year.";
table XY;
run;

 

and also

 

proc means data = drg n mean std;
title "Output Nr. 2: Blablabla, &year.";
var XY;
where A = 1;
run;

 

is the data = drg right? Because it's the name of my data? Or do I have to add the year? Like, how do it do it that the proc means stuff is for each year?

 

data = drg or data = drg&2005 or data = drg&2005. or data = drg_&2005

 

I'm so sorry I guess it's really simple but I just don't get it

 

hannah_hortlik
Obsidian | Level 7

Okay, thanks again.

One last question, I want to do a loop, and also a grouped variable.

So i have ops_ko1 up to ops_ko101, and I want to "loop" it that it's always +1

And I want to say that if ops_ko (no matter which number so ops_ko1-101), if it is = 500 then ops_ko =1, and if ops_ko (no matter which number so ops_ko1-101) ~= 500 then ops_ko = 0

 

The dumb way:

if ops_ko1 ~= 88398 then ops_ko = 0 ;
if ops_ko1 = 88398 then ops_ko = 1;
if ops_ko2 ~= 88398 then ops_ko = 0 ;
if ops_ko2 = 88398 then ops_ko = 1;
if ops_ko3 ~= 88398 then ops_ko = 0 ;
if ops_ko3 = 88398 then ops_ko = 1;

 

and do it for every ops_ko up to 101

 

But the smartsolution should be different right?

Anything like..

ops_ko = 0;
do while (ops_ko<=101);
ops_ko = ops_ko+1;
end;
run;

 

if ops_ko1 ~= 88398 then ops_ko = 0 ;
if ops_ko1 = 88398 then ops_ko = 1;

 

But I don't know how to write it right. Thanks again and have a wonderful day !

Tom
Super User Tom
Super User

@hannah_hortlik wrote:

Okay, thanks again.

One last question, I want to do a loop, and also a grouped variable.

So i have ops_ko1 up to ops_ko101, and I want to "loop" it that it's always +1

And I want to say that if ops_ko (no matter which number so ops_ko1-101), if it is = 500 then ops_ko =1, and if ops_ko (no matter which number so ops_ko1-101) ~= 500 then ops_ko = 0

 

The dumb way:

if ops_ko1 ~= 88398 then ops_ko = 0 ;
if ops_ko1 = 88398 then ops_ko = 1;
if ops_ko2 ~= 88398 then ops_ko = 0 ;
if ops_ko2 = 88398 then ops_ko = 1;
if ops_ko3 ~= 88398 then ops_ko = 0 ;
if ops_ko3 = 88398 then ops_ko = 1;

 

and do it for every ops_ko up to 101

 

But the smartsolution should be different right?

Anything like..

ops_ko = 0;
do while (ops_ko<=101);
ops_ko = ops_ko+1;
end;
run;

 

if ops_ko1 ~= 88398 then ops_ko = 0 ;
if ops_ko1 = 88398 then ops_ko = 1;

 

But I don't know how to write it right. Thanks again and have a wonderful day !


This problem does not need to use macro code.  But it could benefit from using an ARRAY.  An ARRAY is just a handy way to reference multiple variables using a single name with an index.  So to reference your 101 variables you might use an array statement like:

array list ops_ko1-ops_ko101 ;

Then you reference OPS_KO1 as LIST[1], where the 1 could come from a variable.

 

To actually solve your problem you need to explain what you want to do. 

Do you want to replace the value of OP_KO1 with 0 or 1?

do index=1 to dim(list);
  list[index] = list[index] = 88398 ;
end;

Or do you want to make a new variable with a 0 or 1? If a new variable do you want to make 101 new variables? Or just one.  If just one should it be 1 if ANY of the 101 variables have the value and 0 when NONE of the 101 variables have the value? Or something else?

 

 

hannah_hortlik
Obsidian | Level 7

Hey,

 

I just want to have a grouped variable, is that the right way to say it? The same way like:

age2=0

If age < 10 then age2 = 0

If age >= 10 & age < 20 then age2 = 1

If age >= 20 & age < 30 then age2 = 2;

 

ops_ko = 0;
if ops_ko1 ~= 88398 then ops_ko = 0 ;
if ops_ko1 = 88398 then ops_ko = 1;
if ops_ko2 ~= 88398 then ops_ko = 0 ;
if ops_ko2 = 88398 then ops_ko = 1;

 

but now for every ops_ko, up to 101.

 

 

 

 

If I say I want to make a new variable named "ops_ko" which is with all ops_ko1 to ops_ko101. And it should be 1 if ANY of the 101 variables have the value (for example 500) and 0 when NONE of the 101 variables have the value it's a big difference, right?

 

And to your last answer:

My actual data is named „drg2005_sf“ , „drg2006_sf“ for every year.

So you mean I have to write „drg_&year“ in this cases if I want results for each year?

 

proc freq data = drg;
title "Output Nr. 1a: Blablabla &year.";
table XY;
run;

 and also 

proc means data = drg n mean std;
title "Output Nr. 2: Blablabla, &year.";
var XY;
where A = 1;
run

 

 

Kurt_Bremser
Super User

@hannah_hortlik wrote:

Okay, thanks again.

One last question, I want to do a loop, and also a grouped variable.

So i have ops_ko1 up to ops_ko101, and I want to "loop" it that it's always +1

And I want to say that if ops_ko (no matter which number so ops_ko1-101), if it is = 500 then ops_ko =1, and if ops_ko (no matter which number so ops_ko1-101) ~= 500 then ops_ko = 0

 

The dumb way:

if ops_ko1 ~= 88398 then ops_ko = 0 ;
if ops_ko1 = 88398 then ops_ko = 1;
if ops_ko2 ~= 88398 then ops_ko = 0 ;
if ops_ko2 = 88398 then ops_ko = 1;
if ops_ko3 ~= 88398 then ops_ko = 0 ;
if ops_ko3 = 88398 then ops_ko = 1;

 

and do it for every ops_ko up to 101

 

But the smartsolution should be different right?

Anything like..

ops_ko = 0;
do while (ops_ko<=101);
ops_ko = ops_ko+1;
end;
run;

 

if ops_ko1 ~= 88398 then ops_ko = 0 ;
if ops_ko1 = 88398 then ops_ko = 1;

 

But I don't know how to write it right. Thanks again and have a wonderful day !


So you have 101 variables that need to be checked, and just one variable to hold the result. Which exact condition should cause the new variable to be 1, and under which exact condition should it be 0?

hannah_hortlik
Obsidian | Level 7

So you have 101 variables that need to be checked, and just one variable to hold the result.

Yes

 

Which exact condition should cause the new variable to be 1, and under which exact condition should it be 0?

When one of the 101 variables = 500 then it should be 1. If none of the 101 variables = 500 then it should be 0

So if ops_ko1 = 500 then ops_ko (name of the new variable? I don't know) = 1

if ops_ko1 ~= 500 then ops_ko = 0

 

and this should be for every ops_ko, from ops_ko1 to 101

Kurt_Bremser
Super User

So what you will do is this:

  • define an array over these 101 variables
  • set the new variable to zero
  • iterate through the array, and set the new variable to 1 if the desired value is encountered; you can also add code to exit the loop
array _ops_ko {101} ops_ko1-ops_ko101;
ops_ko = 0;
do _i = 1 to 101;
  if _ops_ko{_i} = 88398
  then do;
    ops_ko = 1;
    _i = 200; * forces exit of the loop, for performance;
  end;
end;
drop _i;
Tom
Super User Tom
Super User

Just change the loop.

I would set the variable to false (0) to start with and then stop looping when it becomes true (1).

ops_ko=0;
do index=1 to dim(list) while (ops_ko=0);
   ops_ko = list[index] = 500 ;
end;
Tom
Super User Tom
Super User

Do you actually have a dataset named DRG? Is it the one you want to use?

 

Your previous code was trying to create a dataset name DRG_&YEAR.  Is that they one you want to use?  If so then say so in the PROC FREQ code.

 

Trying to use DRG&2005 is not going to work as dataset names cannot have the character & in their names.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 11 replies
  • 1246 views
  • 2 likes
  • 3 in conversation