BookmarkSubscribeRSS Feed
claudiajc
Obsidian | Level 7

Dear experts,

I am creating the data set ap3_8fmt to create the formats for a variable called ap3_8, contained in the data set tsdemwl. The formats are stored in the work library (I can see that in the results window) but they are not picked up by the data set that contains the variable ap3_8.

Could you please tell me what am I doing wrong?

Many thanks,

Claudia

 

------ CODE------

data ap3_8_;
 input ap3_8 $ @@;
 datalines;
 1 2 3 4 b
;
run;

 

data ap3_8;
 length descrip_ $100;
 set ap3_8_;
         if ap3_8="1" then descrip_="Vendio o hizo algun producto para su venta?";
 else if ap3_8="2" then descrip_="Presto algun servicio a cambio de un pago?";
 else if ap3_8="3" then descrip_="Ayudo trabajando en las tierras o en el negocio de un familiar u otra persona?";
 else if ap3_8="4" then descrip_="Entonces, no realizo otra actividad?";
 else if ap3_8="b" then descrip_="Blanco";
run;

 

data ap3_8fmt;
  length descrip $100;
  set ap3_8 (rename=(ap3_8=start));
  retain fmtname "$ap3_8f";
  label = descrip_;
  descrip=descrip_;
  if substr(descrip,1,1)="¿" then descrip=substr(descrip,2);
run;

 

proc format cntlin=ap3_8fmt;
run;

 

proc format library=work fmtlib;
run;

 

options fmtsearch=(work);

 

data mydatawf;
  length ap3_8wf $100;
  set p1dem.tsdemwl;
  ap3_8wf=put(AP3_8,$AP3_8F.);
run;

 

proc print data = mydatawf (obs=5) noobs
run;

results_window.jpg

 

5 REPLIES 5
PGStats
Opal | Level 21

Works fine for me

 

data ap3_8_;
 input ap3_8 $ @@;
 datalines;
 1 2 3 4 b
;
run;

data ap3_8;
 length descrip_ $100;
 set ap3_8_;
         if ap3_8="1" then descrip_="Vendio o hizo algun producto para su venta?";
 else if ap3_8="2" then descrip_="Presto algun servicio a cambio de un pago?";
 else if ap3_8="3" then descrip_="Ayudo trabajando en las tierras o en el negocio de un familiar u otra persona?";
 else if ap3_8="4" then descrip_="Entonces, no realizo otra actividad?";
 else if ap3_8="b" then descrip_="Blanco";
run;

data ap3_8fmt;
  length descrip $100;
  set ap3_8 (rename=(ap3_8=start));
  retain fmtname "$ap3_8f";
  label = descrip_;
  descrip=descrip_;
  if substr(descrip,1,1)="¿" then descrip=substr(descrip,2);
run;

proc format cntlin=ap3_8fmt;
run;

data mydatawf;
  length ap3_8wf $100;
  set ap3_8_;
  ap3_8wf=put(AP3_8, $AP3_8F.);
run;

proc print data=mydatawf noobs; run;
ap3_8wf                                                                        ap3_8

Vendio o hizo algun producto para su venta?                                      1
Presto algun servicio a cambio de un pago?                                       2
Ayudo trabajando en las tierras o en el negocio de un familiar u otra persona?   3
Entonces, no realizo otra actividad?                                             4
Blanco                                                                           b
PG
claudiajc
Obsidian | Level 7

Hi PGStats,

 

The code you suggests works for me too, but that is because we are adding the formats to a different data set. I am attaching the data set that needs the format. Could I ask you to add formats to this one (dem5.sas7bdat) please?

 

Thanks,

 

Claudia

Reeza
Super User

Change your formula to:

 

ap3_8wf=put(compress(AP3_8),$AP3_8F.);

If this works, it means you have spaces in your character value so the format doesn't exactly match your actual data. 

 

claudiajc
Obsidian | Level 7

Thanks, Reeza. I reran the code using 'compress' and it did not work 😞

ballardw
Super User

@claudiajc wrote:

Dear experts,

I am creating the data set ap3_8fmt to create the formats for a variable called ap3_8, contained in the data set tsdemwl. The formats are stored in the work library (I can see that in the results window) but they are not picked up by the data set that contains the variable ap3_8.

Could you please tell me what am I doing wrong?

Many thanks,

Claudia

 


Nothing in your code shows that the set tsdemwl should use the format. You create mydatawf from  p1dem.tsdemwl and use the format but there is NO reason that  p1dem.tsdemwl should have any connection to the format.

If you try this code after the format is created then you can display the value from that data set with the given format.

proc print data= p1dem.tsdemwl;
   format AP3_8 $AP3_8F.;
run;

Or you can use proc datasets to change the current format of the variable ap3_8 in the data set.

 

Formats must be explicitly assigned, either for use during a procedure, when a variable is created or by modifying an existing data set to change the format. Otherwise there is know way for SAS to know that you want to use a different format than the one assigned when the original data was created.

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
  • 5 replies
  • 1672 views
  • 0 likes
  • 4 in conversation